title: BackgroundAudioManager

sidebar_label: BackgroundAudioManager

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

Reference

Methods

Property Type Read only Description
buffered number Yes The 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).
coverImgUrl string No The 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.
currentTime number Yes The position where the playback has got to (in sec). It is only returned when a valid src attribute exists (read only).
duration number Yes The length of the audio file (in sec), which is only returned when a valid src attribute exists (read only).
epname string No The album name, which is also used in the card description shared via the native audio player.
paused boolean Yes Whether the playback is paused or stopped (read only).
protocol string No The 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.
singer string No The singer name, which is also used in the card description shared via the native audio player.
src string No Indicates 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.
startTime number No The position where the audio playback starts (in sec).
title string No The 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.
webUrl string No The page URL, which is also used in the card description shared via the native audio player.

play

Plays music.

  1. () => void

pause

Pauses music.

  1. () => void

stop

Stops music.

  1. () => void

seek

Jumps to the specific position.

  1. (position: any) => void

onCanplay

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

  1. (callback?: () => void) => void
Property Type
callback () => void

onPlay

Listens on the background audio playback event.

  1. (callback?: () => void) => void
Property Type
callback () => void

onPause

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

  1. (callback?: () => void) => void
Property Type
callback () => void

onStop

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

  1. (callback?: () => void) => void
Property Type
callback () => void

onEnded

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

  1. (callback?: () => void) => void
Property Type
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.

  1. (callback?: () => void) => void
Property Type
callback () => void

onPrev

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

  1. (callback?: () => void) => void
Property Type
callback () => void

onNext

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

  1. (callback?: () => void) => void
Property Type
callback () => void

onError

Listens on the background audio playback error event.

  1. (callback?: () => void) => void
Property Type
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.

  1. (callback?: () => void) => void
Property Type
callback () => void

Sample Code

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