wx.getBackgroundAudioManager() 背景音乐
wx.getBackgroundAudioManager()
推荐大家使用背景音乐不在使用audio
audio播放音频加载时间长 体验需求达不到
相关文档
https://developers.weixin.qq.com/miniprogram/dev/api/media/background-audio/BackgroundAudioManager.html
相关代码
const bgMusic = wx.getBackgroundAudioManager() //创建背景音乐
Page({data: {id: '',//请求数据参数idmusicMsg: {},//接收数据对象},onLoad: function (options) {var that = thisthat.setData({id: options.id,})that.getPicInfo() //请求服务器}, // 开始播放listenerButtonPlay: function (src) {var that = thisconsole.log(src)bgMusic.src = src;bgMusic.onTimeUpdate(() => { //监听音频播放进度console.log(bgMusic.currentTime)})bgMusic.onEnded(() => { //监听音乐自然播放结束console.log("音乐播放结束");that.listenerButtonPlay(that.data.musicMsg.mp3)})bgMusic.play(); //播放音乐},getPicInfo() {var that = thiswx.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) //成功回调执行播放音乐that.setData({musicMsg: res.data.data, //赋值对象})}})},onUnload() {var that = thisthat.listenerButtonStop()//页面卸载时停止播放console.log("离开")},//暂停audioPause: function () {var that = thisbgMusic.pause(); //暂停播放音乐console.log('暂停')},audioPlay: function () {var that = thisbgMusic.play(); //停止播放console.log('继续播放')},//停止播放listenerButtonStop: function () {bgMusic.stop()},
})
喜欢上方小程序,需要源码的,添加博主微信私信小编.