🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ### 1. 创建一个音乐实例 ~~~ //创建一个音乐实例(在有播放的js文件中的顶部) const audio =wx.getBackgroundAudioManager(); ~~~ ### 2. wxml 和js文件配置 ~~~ //wxml (点击触发播放函数同时改变状态图标) //背景图片改变 <image src="{{isplay?list[0].musiccoverImg:list[0].imgSrc}}"></image> //音乐图片改变 <image bindtap = "playmusic" src="{{isplay?'/images/music/music-stop.png':'/images/music/music-start.png'}}" class="music"></image> //js data: { isplay:false, }, ~~~ ### 3. 播放音乐函数(重点) ~~~ js文件中 playmusic(){ var url=this.data.list[0].musicurl; // console.log(url) if(this.data.isplay){ wx.stopBackgroundAudio(); // 停止音频 // audio.onPause(); this.setData({ isplay: false }) }else{ wx.playBackgroundAudio(); //播放音频 // audio.onPlay(); audio.src=url; this.setData({ isplay: true }) } }, ~~~ ### 4.在app.js创建全局变量接收正在播放页面的id和图片的状态 ~~~ globalData:{ g_isPlay:false, //自定义名字 g_currentId:null // 自定义名字 } ~~~ ### 5. 状态同步 创建监听函数 在onload中调用 (此方法已废弃) ~~~ onMusic() { var self = this; //监听音乐播放事件(点击后触发的事件) wx.onBackgroundAudioPlay((result) => { // console.log(1) this.setData({ isplay: true }); app.globalData.g_isPlay=true; //进入页面点击播放将id传到外部的局部变量中 退出当前页面换一个页面进入不会触发该事件 其他的点击事件将数据清空 app.globalData.g_currentId = self.data.list[0].id }); wx.onBackgroundAudioPause((result) => { // console.log(2) this.setData({ isplay: false }) app.globalData.g_isPlay = false; app.globalData.g_currentId = null; }); wx.onBackgroundAudioStop((result)=>{ // console.log(3) self.setData({ isplay: false }); app.globalData.g_currentId = null; }); }, //在onload函数中调用和判断 this.onMusic(); if(app.globalData.g_isPlay && app.globalData.g_currentId==options.id){ this.setData({ isplay:true }); } ~~~ ### 6. 状态同步 创建监听函数 (新方法 常用方法) ~~~ // 音乐暂停监听(页面加载时如果音乐为暂停状态改变音乐的状态图标) //当 音乐暂停 时触发的函数 audio.onPause(() => { // console.log(1) this.setData({ isplay: false }) app.globalData.g_isPlay = false; app.globalData.g_currentId = null; }); //当 音乐结束 时触发的函数 audio.onEnded(() => { // console.log(1) this.setData({ isplay: false }) }); //当音乐 停止播放时 时触发的函数 audio.onStop(() => { // console.log(1) this.setData({ isplay: false }) }); // 音乐播放监听(页面加载时如果音乐为播放状态改变音乐的状态图标) //当 音乐开始播放 时触发的函数 audio.onPlay(() => { // console.log(2) this.setData({ isplay: true }) app.globalData.g_isPlay = true; // 进入页面点击播放将id传到外部的局部变量中 退出当前页面换一个页面进入不会触发该事件 app.globalData.g_currentId = self.data.list[0].id; }); // this.onMusic(); if (app.globalData.g_isPlay && app.globalData.g_currentId == key) { this.setData({ isplay: true }); } ~~~ ### 整体代码展示 ~~~ // pages/details/details.js const loacal = require("../../data/local"); //创建一个音乐实例 const audio = wx.getBackgroundAudioManager(); const app = getApp(); Page({ /** * 页面的初始数据 */ data: { postList: [], list: [], isplay: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var key = options.id; // console.log(key) var self = this; var postList = loacal.postList; var list = []; var temp = {}; temp.author = postList[key].author; temp.dateTime = postList[key].dateTime; temp.detail = postList[key].detail; temp.musicurl = postList[key].music.url; temp.musiccoverImg = postList[key].music.coverImg; temp.imgSrc = postList[key].imgSrc; temp.avatar = postList[key].avatar; temp.title = postList[key].title; //数据下的id temp.id = postList[key].postId; list.push(temp); this.setData({ postList, list, }); // 音乐暂停监听(页面加载时如果音乐为暂停状态改变音乐的状态图标) audio.onPause(() => { // console.log(1) this.setData({ isplay: false }) app.globalData.g_isPlay = false; app.globalData.g_currentId = null; }); // 音乐播放监听(页面加载时如果音乐为播放状态改变音乐的状态图标) audio.onPlay(() => { // console.log(2) this.setData({ isplay: true }) app.globalData.g_isPlay = true; // 进入页面点击播放将id传到外部的局部变量中 退出当前页面换一个页面进入不会触发该事件 app.globalData.g_currentId = self.data.list[0].id; }); // this.onMusic(); if (app.globalData.g_isPlay && app.globalData.g_currentId == key) { this.setData({ isplay: true }); } }, playmusic() { var url = this.data.list[0].musicurl; // console.log(url) if (this.data.isplay) { wx.stopBackgroundAudio(); // 停止音频 audio.onPause(); this.setData({ isplay: false }) } else { wx.playBackgroundAudio(); //播放音频 // audio.onPlay(); audio.src = url; this.setData({ isplay: true }) } }, //进入页面是根据音乐的播放状态来改变音乐状态图标 // onMusic() { // var self = this; // //监听音乐播放事件(点击后触发的事件) // wx.onBackgroundAudioPlay((result) => { // // console.log(1) // this.setData({ // isplay: true // }); // app.globalData.g_isPlay = true; // //进入页面点击播放将id传到外部的局部变量中 退出当前页面换一个页面进入不会触发该事件 // app.globalData.g_currentId = self.data.list[0].id; // }); // wx.onBackgroundAudioPause((result) => { // // console.log(2) // this.setData({ // isplay: false // }) // app.globalData.g_isPlay = false; // app.globalData.g_currentId = null; // }); // wx.onBackgroundAudioStop((result) => { // // console.log(3) // self.setData({ // isplay: false // }); // app.globalData.g_currentId = null; // }); // }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } }) ~~~ ~~~ <!--pages/details/details.wxml--> <image src="{{isplay?list[0].musiccoverImg:list[0].imgSrc}}"></image> <image bindtap = "playmusic" src="{{isplay?'/images/music/music-stop.png':'/images/music/music-start.png'}}" class="music"></image> ~~~