Skip to main content
Version: Next

BackgroundAudioManager

The BackgroundAudioManager instance can be obtained through the API Taro.getBackgroundAudioManager.

Reference

Methods

PropertyTypeRead onlyDescription
bufferednumberYesThe position where the audio is buffered. Only the part between the position where the playback has got to and this position is buffered (read only).
coverImgUrlstringNoThe cover image URL used as the background image of the native audio player. It is also used in the card image and background shared via the native audio player.
currentTimenumberYesThe position where the playback has got to (in sec). It is only returned when a valid src attribute exists (read only).
durationnumberYesThe length of the audio file (in sec), which is only returned when a valid src attribute exists (read only).
epnamestringNoThe album name, which is also used in the card description shared via the native audio player.
pausedbooleanYesWhether the playback is paused or stopped (read only).
protocolstringNoThe protocol used for audio transfer. It is 'http' by default. You can set it to 'hls' for the live stream audio transferred over HLS protocol.
singerstringNoThe singer name, which is also used in the card description shared via the native audio player.
srcstringNoIndicates the audio data source (Cloud file ID is supported as of base library 2.2.3). It is an empty string by default. When a new src attribute is configured, the audio file is played automatically. M4a, AAC, MP3, and WAV file formates are supported.
startTimenumberNoThe position where the audio playback starts (in sec).
titlestringNoThe title of an audio file in the native audio player (required). It is also used as the card title shared via the native audio player.
webUrlstringNoThe page URL, which is also used in the card description shared via the native audio player.

play

Plays music.

() => void

pause

Pauses music.

() => void

stop

Stops music.

() => void

seek

Jumps to the specific position.

(position: any) => void

onCanplay

Listens on the event that an audio file is ready for playback. A smooth playback is not guaranteed.

(callback?: () => void) => void
PropertyType
callback() => void

onPlay

Listens on the background audio playback event.

(callback?: () => void) => void
PropertyType
callback() => void

onPause

Listens on the event of pausing the playback of a background audio file.

(callback?: () => void) => void
PropertyType
callback() => void

onStop

Listens on the event of stopping the playback of a background audio file.

(callback?: () => void) => void
PropertyType
callback() => void

onEnded

Listens on the event of playing an background audio file to the end without interruption.

(callback?: () => void) => void
PropertyType
callback() => void

onTimeUpdate

Listens on the event of updating the background audio playback progress. A callback is executed only if the Mini Program is running at the foreground.

(callback?: () => void) => void
PropertyType
callback() => void

onPrev

Listens on the event of tapping the previous song on the system's music player panel (for iOS only).

(callback?: () => void) => void
PropertyType
callback() => void

onNext

Listens on the event of tapping the next song on the system's music player panel (for iOS only).

(callback?: () => void) => void
PropertyType
callback() => void

onError

Listens on the background audio playback error event.

(callback?: () => void) => void
PropertyType
callback() => void

onWaiting

Listens on the audio loading event. It is triggered when the playback of an audio file stops to load the file due to insufficient data.

(callback?: () => void) => void
PropertyType
callback() => void

Sample Code

const backgroundAudioManager = Taro.getBackgroundAudioManager()
backgroundAudioManager.title = 'At this Very Moment'
backgroundAudioManager.epname = 'At this Very Moment'
backgroundAudioManager.singer = 'Xu Wei'
backgroundAudioManager.coverImgUrl = 'https://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
// Plays automatically after src is set
backgroundAudioManager.src = 'https://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'