[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>
~~~
- 前期准备
- 软件安装配置
- 语法 以及 功能 的实现
- 小程序中的 轮播
- 翻转轮播
- 实现 跳转 页面
- 详谈 跳转页面
- for 循环 渲染 页面(重点)
- 点击 改变 元素内容
- 功能 封装(创建、使用 模板)(重点)
- js模块化(重点)
- if-else实现 三目运算
- 底部导航栏tabBar 实现
- 小程序中的 函数调用 方法
- 小程序中的 block 包裹元素
- 小程序中的 hover事件
- import 标签(重点)
- 其他
- 在本地模拟接口取数据
- 点击跳转 并将该元素的id一起传递给跳转的页面
- 点击详情页显示
- 点击事件(bindtap/catchtap)
- 图片的mode属性
- 跳转页面时实现顶部显示页面标题
- hello world
- 将豆瓣服务器接口设置在本地
- 组件
- 地图
- 下拉刷新
- 数据加载 loading...
- 动态设置导航(title设置)
- 实现js代码的模块化
- 传参
- 组件中的生命周期函数
- 实战
- 发送http请求
- 可用的豆瓣接口
- 处理豆瓣列表页的数据
- 从接口上取数据渲染到页面上1
- 从接口上取数据渲染页面实现瀑布流2
- 瀑布流
- 音乐播放
- 文章详情页
- 音乐播放组件
- 音乐播放 最终版
- 电影(封装取数据渲染)
- 分享与收藏
- 搜索框
- 将电影列表数据放缓存
- 零碎知识点
- 谈组件
- 请求封装 (重点)
- 实现简单需求的请求失败的封装
- 使用class实现显示各种错误信息
- 再次封装带class的请求实现改变里面给的url
- 使用promise 封装http
- promise
- generator
- 01.介绍
- 02. 基本
- 03. 实例
- 04.yield
- asyns
- 01. 介绍
- 02. 使用
- 03. 取豆瓣
- 子组件(模板文件)接收父组件传来的参数并改变其值
- 模块化
- 在模板中提取相同的部分behavior
- 字符串与数组之间的转换
- 子组件向父组件传参
- 谈 triggerEvent
- 整体展示
- 父组件向子组件传wxml (在两个组件比较相似的情况 定义卡 槽传 wxml)
- 传css (在父组件中定义子组件的样式)
- 使用wxs给wxml传js
- 点赞
- 小程序中的正则
- 组件中实现下拉刷新
- 用户授权
- 组件点击图片获取信息
- 说明
- 小程序上下滑动