WeChat Open Platform

Mini Programs Documentation

Music Controls

wx.getBackgroundAudioPlayerState(OBJECT)

Gets background music playback status.

OBJECT parameter descriptions:

Parameter Type Required Description
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Description
duration Selected audio duration (unit: s), only returned when there is music playing
currentPosition Selected audio playback position (unit: s), only returned when there is music playing
status Playback status (2: no music playing, 1: playing, 0: paused)
downloadPercent Audio download progress (integer, 80 represents 80%), only returned when there is music playing
dataUrl Song data link, only returned when there is music playing

Sample code:

wx.getBackgroundAudioPlayerState({
    success: function(res) {
        var status = res.status
        var dataUrl = res.dataUrl
        var currentPosition = res.currentPosition
        var duration = res.duration
        var downloadPercent = res.downloadPercent
    }
})

wx.playBackgroundAudio(OBJECT)

Uses background player to play music. WeChat clients can only have one piece of background music playing at the same time. The music will be paused after the user leaves the Mini Program. The music will not be paused if the user clicks on "Display at top of chat list". When the user uses a music player in another Mini Program, the music in the original Mini Program will stop playing.

OBJECT parameter descriptions

Parameter Type Required Description
dataUrl String Yes Music link
title String No Music title
coverImgUrl String No Cover URL
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code

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

wx.pauseBackgroundAudio()

Pauses music.

Sample code

wx.pauseBackgroundAudio()

wx.seekBackgroundAudio(OBJECT)

Controls music playback progress.

OBJECT parameter descriptions

Parameter Type Required Description
position Number Yes Music position, unit: seconds
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code

wx.seekBackgroundAudio({
    position: 30
})

wx.stopBackgroundAudio()

Stops music playback.

Sample code

wx.stopBackgroundAudio()

wx.onBackgroundAudioPlay(CALLBACK)

Monitors music playback.

wx.onBackgroundAudioPause(CALLBACK)

Monitors pausing of music.

wx.onBackgroundAudioStop(CALLBACK)

Monitors stopping of music.

Bugs & Tips

  1. bug: wx.seekBackgroundAudio will have short delays in iOS WeChat version 6.3.30.