tencent cloud

Feedback

Background audio

Last updated: 2024-03-05 15:08:43

    stopBackgroundAudio

    This API is used via wx.stopBackgroundAudio(Object object).
    Feature Description: Stops the playback of music.
    Parameter and Description: Object.
    Attribute
    Type
    Default value
    Required
    Note
    success
    function
    -
    No
    Callback Function of Successful Interface Call
    fail
    function
    -
    No
    Callback Function of Failing Interface Call
    complete
    function
    -
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)

    seekBackgroundAudio

    This API is used via wx.seekBackgroundAudio(Object object).
    Feature Description: Controls the progression of music playback.
    Parameter and Description: Object.
    Attribute
    Type
    Default value
    Required
    Note
    position
    number
    -
    Yes
    Music position, measured in seconds
    success
    function
    -
    No
    Callback Function of Successful Interface Call
    fail
    function
    -
    No
    Callback Function of Failing Interface Call
    complete
    function
    -
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
    Sample Code
    wx.seekBackgroundAudio({
    position: 30
    })

    playBackgroundAudio

    This API is used via wx.playBackgroundAudio(Object object).
    Feature Description: Uses the backend player for music playback. For the host client, only one backend 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.
    Parameter and Description: Object.
    Attribute
    Type
    Default value
    Required
    Note
    dataUrl
    string
    -
    Yes
    Music link, currently supporting formats such as m4a, aac, mp3, wav.
    title
    string
    -
    No
    Music Title
    coverImgUrl
    string
    -
    No
    Cover URL
    success
    function
    -
    No
    Callback Function of Successful Interface Call
    fail
    function
    -
    No
    Callback Function of Failing Interface Call
    complete
    function
    -
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
    Sample Code
    wx.playBackgroundAudio({
    dataUrl: '',
    title: '',
    coverImgUrl: ''
    })

    pauseBackgroundAudio

    This API is used via wx.pauseBackgroundAudio(Object object).
    Feature Description: Pauses music playback.
    Parameter and Description: Object.
    Attribute
    Type
    Default value
    Required
    Note
    success
    function
    -
    No
    Callback Function of Successful Interface Call
    fail
    function
    -
    No
    Callback Function of Failing Interface Call
    complete
    function
    -
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)

    onBackgroundAudioStop

    This method is used via wx.onBackgroundAudioStop(function listener).
    Feature Description: Monitors music stop events.
    Parameter and Description: function listener, the listener function for music stop events.

    onBackgroundAudioPlay

    This method is used via wx.onBackgroundAudioPlay(function listener).
    Feature Description: Monitors music playback events.
    Parameter and Description: function listener, the listener function for music playback events.

    onBackgroundAudioPause

    This method is used via wx.onBackgroundAudioPause(function listener).
    Feature Description: Monitors music pause events.
    Parameter and Description: function listener, the listener function for music pause events.

    getBackgroundAudioPlayerState

    This API is used via wx.getBackgroundAudioPlayerState(Object object).
    Feature Description: Gets the backend music playback status.
    Parameter and Description: Object.
    Attribute
    Type
    Default value
    Required
    Note
    success
    function
    -
    No
    Callback Function of Successful Interface Call
    fail
    function
    -
    No
    Callback Function of Failing Interface Call
    complete
    function
    -
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
    Parameters for object.success callback function: Object res.
    Attribute
    Type
    Note
    duration
    number
    The length of the selected audio (in seconds), returned only during music playback.
    currentPosition
    number
    The playback position of the selected audio (in seconds), returned only during music playback via getBackgroundAudioManager.
    status
    number
    Playback status, whose valid values include:
    0: Pausing
    1: Playing
    2: No music being played
    downloadPercent
    number
    The percentage of the audio download progress, returned only during music playback.
    dataUrl
    string
    Song data link, returned only during music playback.
    Sample 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

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

    BackgroundAudioManager

    BackgroundAudioManager instance, which can be obtained through wx.getBackgroundAudioManager.

    Attribute

    Type
    Note
    string src
    The data source of the audio. By default, it is an empty string. When a new src is set, it will automatically start playing. The currently supported formats are m4a, aac, mp3, wav.
    number startTime
    The position where the audio starts playing, in seconds.
    string title
    Audio title, used for the native audio player's audio title (required). The sharing feature in the native audio player and the title of the shared card will also use this value.
    string epname
    Album name, also used by the sharing feature in the native audio player and the description of the shared card.
    string singer
    Artist name, also used by the sharing feature in the native audio player and the description of the shared card.
    string coverImgUrl
    Cover image URL, used for the native audio player background. The shared card and background for the sharing feature in the native audio player will also use this image.
    string webUrl
    Page link, also used by the sharing feature in the native audio player and the description of the shared card.
    string protocol
    Audio protocol, with a default value of 'http'. Setting it to 'hls' can support the playback of live audio using the HLS protocol.
    number playbackRate
    Playback speed, ranging from 0.5 to 2.0, defaulting to 1 (Android requires Version 6 or later).
    number duration
    The length of the current audio (in seconds), only returned when there is a valid src (read-only).
    number currentTime
    The current playback position of the audio (in seconds), only returned when there is a valid src (read-only).
    boolean paused
    Indicates whether the current status is paused or stopped (read-only).
    number buffered
    The time point that the audio is buffered to, ensuring content from the current playback point to this point has been buffered (read-only).
    string referrerPolicy
    origin: Sends the complete referrer; no-referrer: Not to send. The format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, where {appid} is the appid of the mini program, {version} is the version number of the mini program. A version number of 0 indicates a development version, trial version, or review version. A version number of devtools indicates a developer tool, and the rest are official versions.

    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'
    
    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 avaliable.

    7x24 Phone Support