tencent cloud

All product documents
Tencent Cloud Super App as a Service
Background Audio
Last updated: 2024-11-28 10:40:29
Background Audio
Last updated: 2024-11-28 10:40:29

stopBackgroundAudio

The method of utilizing this API is wx.stopBackgroundAudio(Object object).
Function Description: Halts the playback of music.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)

seekBackgroundAudio

The method for utilizing this API is wx.seekBackgroundAudio(Object object)
Function Description: Controls the progression of music playback.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
position
number
-
Supported
Music position, measured in seconds
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
Example Code
wx.seekBackgroundAudio({
position: 30
})

playBackgroundAudio

The method for employing this API is wx.playBackgroundAudio(Object object)
Function Description: Utilizes the background player for music playback. For the host client, only one background music can be played at a time. When the user exits the mini program, the music will pause; if the music player is occupied by another mini program, the music within the original mini program will cease to play.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
dataUrl
string
-
Supported
Music link, currently supporting formats include m4a, aac, mp3, wav
title
string
-
Not required
Music Title
coverImgUrl
string
-
Not required
Cover URL
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
Example Code
wx.playBackgroundAudio({
dataUrl: '',
title: '',
coverImgUrl: ''
})

pauseBackgroundAudio

The method for utilizing this API is wx.pauseBackgroundAudio(Object object)
Function Description: Pauses music playback.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)

onBackgroundAudioStop

The method for utilizing this API is wx.onBackgroundAudioStop(function listener)
Function Description: Monitors music stop events.
Parameters and Description: function listener, the monitoring function for music stop events.

onBackgroundAudioPlay

The method for utilizing this API is wx.onBackgroundAudioPlay(function listener)
Function Description: Monitors music playback events.
Parameters and Description: function listener, the monitoring function for music playback events.

onBackgroundAudioPause

The method for utilizing this API is wx.onBackgroundAudioPause(function listener)
Function Description: Monitors music pause events.
Parameters and Description: function listener, the monitoring function for music pause events.

getBackgroundAudioPlayerState

The method for utilizing this API is wx.getBackgroundAudioPlayerState(Object object)
Function Description: Retrieves the status of background music playback.
Parameters and Description: Object object.
Attributes
Types
Default value
Required
Note
success
function
-
Not required
Callback function for successful interface invocation
fail
function
-
Not required
Callback function for unsuccessful interface invocation
complete
function
-
Not required
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
object.success Callback Function Parameters: Object res.
Attributes
Types
Note
duration
number
The length of the selected audio (in seconds), returned only during music playback.
currentPosition
number
Selects the playback position of the audio (in s), only returned when the music is playing
status
number
Playback status, valid values:
0: Paused
1: Playing
2: No music playing
downloadPercent
number
The percentage of audio download progress, returned only during music playback.
dataUrl
string
Song data link, returned only during music playback.
Example Code
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

Function Description: Acquire the globally unique background audio manager. When the mini program switches to the background, if the audio is playing, it can continue to play. However, the background status cannot manipulate the audio playback status through API calls.

BackgroundAudioManager

BackgroundAudioManager instance, can be obtained through wx.getBackgroundAudioManager.

Attributes

Types
Note
string src
The data source of the audio. Default is empty string, when new src is set, it will start playing automatically, currently supported formats are m4a, aac, mp3, wav
number startTime
The position where the audio starts playing, unit: s.
string title
Audio title, used for the native audio player's audio title (required). The share function in the native audio player, the title of the shared card, will also use this value.
string epname
Album name, the share function in the native audio player, the description of the shared card, will also use this value.
string singer
Singer's name, the share function in the native audio player, the description of the shared card, will also use this value.
string coverImgUrl
Cover image URL, used as the background image for the native audio player. The share function in the native audio player, the accompanying image and background of the shared card will also use this image.
string webUrl
Page link, the share function in the native audio player, the description of the shared card, will also use this value.
string protocol
Audio protocol. The default value is 'http', setting 'hls' can support the playback of live audio using the HLS protocol.
number playbackRate
Playback speed. Range 0.5-2.0, default is 1 (Android requires version 6 or above).
number duration
The length of the current audio (unit: s), only returned when there is a valid src (read-only).
number currentTime
The current playback position of the audio (unit: s), only returned when there is a valid src (read-only).
boolean paused
Whether it is currently paused or stopped (read-only).
number buffered
The time the audio has been buffered, it only guarantees that the content from the current playback time to this point has been buffered (read-only).

Method Set

Sample code

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'

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon