Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat Small Program API Background Audio BackgroundAudioManager instance


May 18, 2021 WeChat Mini Program Development Document


Table of contents


BackgroundAudioManager

BackgroundAudioManager instance, available at wx.getBackgroundAudioManager.

Property

string src

Audio data source (2.2.3 starts supporting cloud file IDs). The default is an empty string, which starts playing automatically when a new src is set, and currently supports formats such as m4a, aac, mp3, wav.


number startTime

Where the audio starts playing, in s.


string title

Audio title for native audio player audio title (required). The sharing feature in the native audio player, shared out of the card title, will also use this value.


string epname

The album name, the sharing feature in the native audio player, the card description shared out, will also use this value.


string singer

The singer's name, the sharing feature in the native audio player, will also use the value for the card profile shared out.


string coverImgUrl

Cover image URL, used to make a background map of the native audio player. The sharing feature in the native audio player, the shared card drawings and background will also be used.


string webUrl

Page links, sharing features in native audio players, shared card profiles, will also use this value.


string protocol

Base library 1.9.94 starts to support, and low versions need to be compatible.

The audio protocol. The default is 'http', and setting 'hls' can support the playback of live audio for the HLS protocol.


number playbackRate

Bastronomy 2.11.0 began support, low version needs to do Compatibility

Play speed.Range 0.5-2.0, default is 1.(Android requires 6 and above version)


number duration

The length of the current audio (unit: s), only returns when there is legal SRC.(Read only)


number currentTime

The current audio playing position (unit: s), only returns when there is legal SRC.(Read only)


boolean paused

Whether it is currently suspended or stopped.(Read only)


number buffered

The time the audio is buffered, only that the content is buffered from the current playback point in time to this point in time. (Read-only)



Method:

BackgroundAudioManager.onCanplay(function callback)

Listen for background audio to enter a playable state event. However, there is no guarantee that the back will play smoothly

Parameters

function callback

The callback function for background audio that enters a playable state event


BackgroundAudioManager.onEnded(function callback)

Listen for background audio to naturally play the end event

Parameters

function callback

Background audio naturally plays the callback function that ends the event


BackgroundAudioManager.onError(function callback)

Listen for background audio playback error events

Parameters

function callback

The callback function for background audio playback error events


BackgroundAudioManager.onNext(function callback)

Listen to the user tap the next event (iOS only) on the system music playback panel

Parameters

function callback

The user taps the callback function for the next event on the system music playback panel


BackgroundAudioManager.onPause(function callback)

Listen for background audio pause events

Parameters

function callback

The callback function for the background audio pause event


BackgroundAudioManager.onPlay(function callback)

Listen for background audio playback events

Parameters

function callback

The callback function for background audio playback events


BackgroundAudioManager.onPrev(function callback)

Listen to the user tap the last song event (iOS only) on the system music playback panel

Parameters

function callback

The user taps the callback function of the last event on the system music playback panel


BackgroundAudioManager.onSeeked(function callback)

Listen for background audio to complete the jump operation event

Parameters

function callback

The callback function for the background audio completion jump operation event


BackgroundAudioManager.onSeeking(function callback)

Listen for background audio to start the jump operation event

Parameters

function callback

The background audio starts to jump the callback function of the action event


BackgroundAudioManager.onStop(function callback)

Listen for background audio stop events

Parameters

function callback

The callback function for the background audio stop event


BackgroundAudioManager.onTimeUpdate(function callback)

Listen for background audio to play progress update events, and only small programs call back when they are in the fore desk.

Parameters

function callback

The callback function for the background audio playback progress update event


BackgroundAudioManager.onWaiting(function callback)

Listen for events in the audio load. It fires when audio needs to stop loading because there is not enough data

Parameters

function callback

The callback function for events in the audio load



BackgroundAudioManager.pause()

Pause the music

Error

Error code The error message Description
10001 System error
10002 Network error
10003 The file is wrong
10004 The format is wrong
-1 Unknown error



BackgroundAudioManager.play()

Play music

Error

Error code The error message Description
10001 System error
10002 Network error
10003 The file is wrong
10004 The format is wrong
-1 Unknown error



BackgroundAudioManager.seek(number currentTime)

Jump to the specified location

Parameters

number currentTime

Jump position, unit s. 3 bits after the exact number of points, which supports ms-level accuracy

Error

Error code The error message Description
10001 System error
10002 Network error
10003 The file is wrong
10004 The format is wrong
-1 Unknown error



BackgroundAudioManager.stop()

Stop the music

Error

Error code The error message Description
10001 System error
10002 Network error
10003 The file is wrong
10004 The format is wrong
-1 Unknown error

The sample code

const backgroundAudioManager = wx.getBackgroundAudioManager()

backgroundAudioManager.title = '此时此刻'
backgroundAudioManager.epname = '此时此刻'
backgroundAudioManager.singer = '许巍'
backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
// 设置了 src 之后会自动播放
backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'