十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
wx.getBackgroundAudioManager()
推薦大家使用背景音樂不在使用audio
audio播放音頻加載時間長 體驗需求達不到
相關(guān)文檔
https://developers.weixin.qq.com/miniprogram/dev/api/media/background-audio/BackgroundAudioManager.html
相關(guān)代碼
const bgMusic = wx.getBackgroundAudioManager() //創(chuàng)建背景音樂 Page({ data: { id: '',//請求數(shù)據(jù)參數(shù)id musicMsg: {},//接收數(shù)據(jù)對象 }, onLoad: function (options) { var that = this that.setData({ id: options.id, }) that.getPicInfo() //請求服務(wù)器 }, // 開始播放 listenerButtonPlay: function (src) { var that = this console.log(src) bgMusic.src = src; bgMusic.onTimeUpdate(() => { //監(jiān)聽音頻播放進度 console.log(bgMusic.currentTime) }) bgMusic.onEnded(() => { //監(jiān)聽音樂自然播放結(jié)束 console.log("音樂播放結(jié)束"); that.listenerButtonPlay(that.data.musicMsg.mp3) }) bgMusic.play(); //播放音樂 }, getPicInfo() { var that = this wx.request({ url: 'https://xxxxx.xxxxx.com/1.json?id=' + that.data.id, header: { 'content-type': 'application/json' }, method: 'GET', success: function (res) { that.listenerButtonPlay(res.data.data.mp3) //成功回調(diào)執(zhí)行播放音樂 that.setData({ musicMsg: res.data.data, //賦值對象 }) } }) }, onUnload() { var that = this that.listenerButtonStop()//頁面卸載時停止播放 console.log("離開") }, //暫停 audioPause: function () { var that = this bgMusic.pause(); //暫停播放音樂 console.log('暫停') }, audioPlay: function () { var that = this bgMusic.play(); //停止播放 console.log('繼續(xù)播放') }, //停止播放 listenerButtonStop: function () { bgMusic.stop() }, })