Skip to content

Background Audio

stopBackgroundAudio

TIP

The API usage is as follows: wx.stopBackgroundAudio(Object object)

  • Functional description: Stop playing music.
  • Parameters and descriptions: Object object
    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)

seekBackgroundAudio

TIP

The API usage is as follows: wx.seekBackgroundAudio(Object object)

  • Functional description: Control the progress of music playback.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    positionnumber-YesMusic position, unit: seconds
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.seekBackgroundAudio({  
    position: 30
})

playBackgroundAudio

TIP

The API usage is as follows: wx.playBackgroundAudio(Object object)

  • Functional description: Use the background player to play music. For the host client, only one background music can be played at the same time. When the user leaves the mini program, the music will be paused; when the user occupies the music player in other mini programs, the music in the original mini program will stop playing.

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    dataUrlstring-YesMusic link, currently supported formats are m4a, aac, mp3, wav
    titlestring-NoMusic title
    coverImgUrlstring-NoCover URL
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.playBackgroundAudio({
  dataUrl: '',
  title: '',
  coverImgUrl: ''
})

pauseBackgroundAudio

TIP

The API usage is as follows: wx.pauseBackgroundAudio(Object object)

  • Functional description: Pause playing music.
  • Parameters and descriptions: Object object
    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)

onBackgroundAudioStop

TIP

The API usage is as follows: wx.onBackgroundAudioStop(function listener)

  • Functional description: Listen for music stop events.
  • Parameters and descriptions: function listener,Listen for music stop events.

onBackgroundAudioPlay

TIP

The API usage is as follows: wx.onBackgroundAudioPlay(function listener)

  • Functional description: Listen for music play events.
  • Parameters and descriptions: function listener,Listen for music play events.

onBackgroundAudioPause

TIP

The API usage is as follows: wx.onBackgroundAudioPause(function listener)

  • Functional description: Listen for music pause events.
  • Parameters and descriptions: function listener,Listen for music pause events.

getBackgroundAudioPlayerState

TIP

The API usage is as follows: wx.getBackgroundAudioPlayerState(Object object)

  • Functional description: Get the background music playback status.

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    durationnumberThe length of the selected audio (unit: s), only returned when the music is playing
    currentPositionnumberThe playback position of the selected audio (unit: s), only returned when the music is playing
    statusnumberPlayback status, legal values:
    0: Paused
    1: Playing
    2: No music is playing
    downloadPercentnumberThe percentage of the download progress of the audio, only returned when the music is playing
    dataUrlstringSong data link, only returned when the music is playing
  • Sample code:

js
wx.getBackgroundAudioPlayerState({
  success (res) {
    const status = res.status
    const dataUrl = res.dataUrl
    const currentPosition = res.currentPosition
    const duration = res.duration
    const downloadPercent = res.downloadPercent
  }
})

getBackgroundAudioManager

  • Functional description: Get the globally unique background audio manager. When the mini program enters the background, if the audio is in the playing state, it can continue to play. However, the background state cannot manipulate the audio playback state by calling the API.

BackgroundAudioManager

BackgroundAudioManager instance, which can be used wx.getBackgroundAudioManager obtain.

Properties

TypeDescription
string srcThe data source of the audio. The default is an empty string. When a new src is set, it will automatically start playing. Currently supported formats are m4a, aac, mp3, wav
number startTimeThe position where the audio starts playing, unit: s
string titleAudio title, used for the audio title of the native audio player (required). The sharing function in the native audio player, the shared card title, will also use this value
string epnameAlbum name, the sharing function in the native audio player, the shared card introduction, will also use this value
string singerSinger name, the sharing function in the native audio player, the shared card introduction, will also use this value
string coverImgUrlCover image URL, used as the background image of the native audio player. The sharing function in the native audio player, the shared card illustration and background will also use this image
string webUrlPage link, the sharing function in the native audio player, the shared card introduction, will also use this value
string protocolAudio protocol. The default value is 'http', setting 'hls' can support playing live audio of the HLS protocol
number playbackRatePlayback speed. Range 0.5-2.0, default is 1 (Android requires version 6 and above)
number durationThe length of the current audio (unit: s), only returned when there is a valid src (read-only)
number currentTimeThe current audio playback position (unit: s), only returned when there is a valid src (read-only)
boolean pausedWhether it is currently paused or stopped (read-only)
number bufferedThe time that the audio has been buffered, only guaranteeing that the content has been buffered from the current playback time point to this time point (read-only)

Method set

onCanplay

TIP

The API usage is as follows: BackgroundAudioManager.onCanplay(function listener)

  • Functional description: Monitor the background audio entering the playable state event. But it does not guarantee that it can be played smoothly afterwards.
  • Parameters and descriptions: function listener,Monitoring function of the background audio entering the playable state event.
onEnded

TIP

The API usage is as follows: BackgroundAudioManager.onEnded(function listener)

  • Functional description: Monitoring background audio natural playback end event.
  • Parameters and descriptions: function listener,Monitoring function of the background audio natural playback end event.
onError

TIP

The API usage is as follows: BackgroundAudioManager.onError(function listener)

  • Functional description: Monitoring background audio playback error event.
  • Parameters and descriptions: function listener,Monitoring function of the background audio playback error event.
onNext

TIP

The API usage is as follows: BackgroundAudioManager.onNext(function listener)

  • Functional description: Monitoring user clicks the next song event in the system music playback panel (iOS only).
  • Parameters and descriptions: function listener,Monitoring function of the user clicks the next song event in the system music playback panel.
onPause

TIP

The API usage is as follows: BackgroundAudioManager.onPause(function listener)

  • Functional description: Monitoring background audio pause event.
  • Parameters and descriptions: function listener,Monitoring function of the background audio pause event.
onPlay

TIP

The API usage is as follows: BackgroundAudioManager.onPlay(function listener)

  • Functional description: Monitoring background audio playback event.
  • Parameters and descriptions: function listener,Monitoring function of the background audio playback event.
onPrev

TIP

The API usage is as follows: BackgroundAudioManager.onPrev(function listener)

  • Functional description: Listen for the event that the user clicks the previous song on the system music player panel (iOS only).
  • Parameters and descriptions: function listener,Listen for the event that the user clicks the previous song on the system music player panel.
onSeeked

TIP

The API usage is as follows: BackgroundAudioManager.onSeeked(function listener)

  • Functional description: Listen for the event that the background audio completes the jump operation.
  • Parameters and descriptions: function listener,Listen for the event that the background audio completes the jump operation.
onSeeking

TIP

The API usage is as follows: BackgroundAudioManager.onSeeking(function listener)

  • Functional description: Listen for the event that the background audio starts jumping.
  • Parameters and descriptions: function listener,Listen for the event that the background audio starts jumping.
onStop

TIP

The API usage is as follows: BackgroundAudioManager.onStop(function listener)

  • Functional description: Listen for the event that the background audio stops.
  • Parameters and descriptions: function listener,Listen for the event that the background audio stops.
onTimeUpdate

TIP

The API usage is as follows: BackgroundAudioManager.onTimeUpdate(function listener)

  • Functional description: Listen for the event that the background audio playback progress is updated. It will only be called back when the applet is in the foreground.
  • Parameters and descriptions: function listener,Listen for the event that the background audio playback progress is updated.
onWaiting

TIP

The API usage is as follows: BackgroundAudioManager.onWaiting(function listener)

  • Functional description: Listen for the event that the audio is loading. It will be triggered when the audio needs to stop loading due to insufficient data.
  • Parameters and descriptions: function listener,Listen for the event that the audio is loading.
pause

TIP

The API usage is as follows: BackgroundAudioManager.pause()

  • Functional description: Pause music

  • Error code

    Error codeError messageDescription
    10001-System error
    10002-Network error
    10003-File error, please check whether the response header lacks Content-Length
    10004-Format error
    -1-Unknown error
play

TIP

The API usage is as follows: BackgroundAudioManager.play()

  • Functional description: Play music
seek

TIP

The API usage is as follows: BackgroundAudioManager.seek(number currentTime)

  • Functional description: Jump to the specified position

  • Parameters and descriptions: number currentTime,The jump position, in s, accurate to 3 decimal places, that is, supports ms-level accuracy.

  • Error code

    Error codeError messageDescription
    10001-System error
    10002-Network error
    10003-File error, please check whether the response header lacks Content-Length
    10004-Format error
    -1-Unknown error
stop

TIP

The API usage is as follows: BackgroundAudioManager.stop()

  • Functional description: Stop music

  • Error code

    Error codeError messageDescription
    10001-System error
    10002-Network error
    10003-File error, please check whether the response header lacks Content-Length
    10004-Format error
    -1-Unknown error

Sample code:

js
const backgroundAudioManager = wx.getBackgroundAudioManager()

backgroundAudioManager.title = 'This Very Moment'
backgroundAudioManager.epname = 'This Very Moment'
backgroundAudioManager.singer = 'Xu Wei'
backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
// After setting the src, it will play automatically.
backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'