iOS SDK ≥ 1.1.2, Android is not supported yet
- Function: Retrieves the globally unique background audio manager.
- Contact: This interface capability is an extension of the API with the same name provided by Mini Program. It is compatible with the properties and methods of the same-name API in Mini Program. Click to view details.
- Features: Supports synchronously displaying the playback panel in the system Control Center when the App is playing audio; supports audio playback continuing after the phone screen is locked.
Notes
WeChat DevTools: Before invoking this API capability, you must enable "Media SDK" and "App Configuration (info.plist-related)" in project.miniapp.json, as shown in the figures below.
Return Value
Returns an instance of BackgroundAudioManager, which can be obtained via wx.getBackgroundAudioManager().
The following only lists the attributes and methods that are required for the new capabilities of this interface. For other attributes and methods, please refer to the Mini Program documentation.
Attributes
-
string src
The data source of the audio (Cloud File ID has been supported since 2.2.3). The default value is an empty string. When a new src is set, playback will start automatically. Currently supported formats include m4a, aac, mp3, and wav.
-
string title
The audio title, used for the audio title in the native audio player (required). The title of the card shared via the share function in the native audio player will also use this value.
Methods
-
BackgroundAudioManager.onNext(function listener)
Listens for the event when the user taps the next track button in the system music playback panel (iOS only)
-
BackgroundAudioManager.onPrev(function listener)
Listens for the event when the user taps the previous track button in the system music playback panel (iOS only)
Sample Code
onLoad() {
const backgroundAudioManager = wx.getBackgroundAudioManager()
backgroundAudioManager.onPause(() => {
console.log('Pause')
})
backgroundAudioManager.onPlay(() => {
console.log('Play')
})
backgroundAudioManager.onNext(() => {
console.log('User tapped "Next" in the Control Center')
})
backgroundAudioManager.onPrev(() => {
console.log('User tapped "Previous" in the Control Center')
})
},
play() {
const backgroundAudioManager = wx.getBackgroundAudioManager()
backgroundAudioManager.title = 'At This Moment'
backgroundAudioManager.singer = 'Xu Wei'
// Playback starts automatically after setting src
backgroundAudioManager.src = 'https://xxx/xxx.mp3'
}