MENU

小程序添加背景音乐配置详解

January 22, 2020 • javascript

官方文档:

代码示意:

bg_music() {
            const bg_music = wx.getBackgroundAudioManager();
            bg_music.title = 'one by one';//音乐名
            bg_music.singer = 'Enya';//作者名
            bg_music.epname = 'The Very Best Of Enya';//专辑名
            bg_music.coverImgUrl = 'https://www.zhai78.com/usr/uploads/2020/01/1946191900.jpg';//封面地址
            bg_music.src = 'https://www.zhai78.com/usr/uploads/2020/01/one_by_one.mp3'; //音乐地址
            bg_music.onEnded(() => {
                  bg_music.play();
            })
      },

使用方法:

  • 建议在app.js中直接调用上述函数,示例如下:
//app.js
App({
      //小程序首次启动
      onLaunch: function() {
            this.bg_music();
      },
      //背景音乐
      bg_music() {
            const bg_music = wx.getBackgroundAudioManager();
            bg_music.title = 'one by one'; //音乐名
            bg_music.singer = 'Enya'; //作者名
            bg_music.epname = 'The Very Best Of Enya'; //专辑名
            bg_music.coverImgUrl = 'https://www.zhai78.com/usr/uploads/2020/01/1946191900.jpg'; //封面地址
            bg_music.src = 'https://www.zhai78.com/usr/uploads/2020/01/one_by_one.mp3'; //音乐地址
            bg_music.onEnded(() => {
                  bg_music.play();
            })
      },
})

其它注意:

  • 若需要小程序在退到后台后继续播放音频,你需要在app.json中配置requiredBackgroundModes属性
"requiredBackgroundModes": ["audio"]
转载请注明出处,并添加原文地址

Archives QR Code Tip
QR Code for this page
Tipping QR Code