WeChat applet and uni.app background playback

Both the WeChat applet and the uni.app framework support playing audio in the background, which can be achieved in the following two ways:

  1. Create the background audio manager object through  wx.getBackgroundAudioManager() the method, and then call the related method to control the playback and pause of the audio. For example, the following code realizes playing a network audio in the background:
    const backgroundAudioManager = wx.getBackgroundAudioManager();
    backgroundAudioManager.src = 'http://music.163.com/song/media/outer/url?id=322935.mp3'; // 音频文件的网络地址
    backgroundAudioManager.title = '歌曲名称'; // 歌曲名称
    backgroundAudioManager.coverImgUrl = 'http://图片地址.jpg'; // 封面图地址
    backgroundAudioManager.play();
    

  2. In  uni-app , you can  App.vue declare  onShow and  onHide lifecycle functions in , to control the playback and pause of audio when the applet is in the foreground and background respectively. For example:
    // App.vue
    export default {
      onShow() {
        uni.getBackgroundAudioManager().play(); // 背景音频播放
      },
      onHide() {
        uni.getBackgroundAudioManager().pause(); // 背景音频暂停
      }
    }
    

    The above two methods can achieve the effect of playing audio in the background in the applet or uni.app. It should be noted that some interfaces of WeChat applets need to apply for permission to use through WeChat official registration. At the same time, when using it in uni.app, you need to  manifest.json apply for this permission in the file.

Guess you like

Origin blog.csdn.net/qq_19820589/article/details/131015315