問題: html
想在打開小程序時就自動播放背景音樂(循環)小程序
解決方法:api
一、思路:寫一個函數,在 onLoad()中調用函數
二、this
1 //index.js 2 //獲取應用實例 3 const back = wx.getBackgroundAudioManager(); 4 5 Page({ 6 ...................... 7 // 音樂播放函數 8 backmusic: function () { 9 player(); 10 function player() { 11 back.title = "羅密歐與朱麗葉 "; // 必需要有一個title 12 back.src = "http://******.mp3"; 13 // 千與千尋 "http://*************.mp3" 14 back.onEnded(() => { 15 player(); // 音樂循環播放 16 }) 17 } 18 }, 19 20 onLoad: function () { 21 //背景音樂播放 22 this.backmusic(); 23 ................... 24 })
三、重點:spa
BackgroundAudioManager 實例 官方連接:https://developers.weixin.qq.com/miniprogram/dev/api/media/background-audio/BackgroundAudioManager.htmlcode
:獲取全局惟一的背景音頻管理器。 小程序切入後臺,若是音頻處於播放狀態,能夠繼續播放。可是後臺狀態不能經過調用API操縱音頻的播放狀態。 htm
BackgroundAudioManager 實例,可經過 wx.getBackgroundAudioManager 獲取。 blog
BackgroundAudioManager.onEnded(function callback)
:監聽背景音頻天然播放結束事件事件
參數 function callback
:背景音頻天然播放結束事件的回調函數