//index.js
const app = getApp();
const items = require('../../data/index');
Page({
data: {
// 歌曲列表
items: items,
// 当前下标
current:0,
// 当前对象
item: null,
// 状态控制
playing: false,
// 是否关闭
stoped:false,
// 总共时间
totalTime:0,
// 当前时间
currentTime:0,
// 状态栏高度
statusBarHeight:app.globalData.statusBarHeight,
// 标题栏高度
titleBarHeight:app.globalData.titleBarHeight,
},
// 加载完成事件
onLoad() {
this.loadMusic(0);
},
// 轮播改变事件
handleSwiperChange(e) {
const current = e.detail.current;
this.setData({current:current},()=>{
this.loadMusic(this.data.current);
});
},
// 播放按钮点击事件
handlePlay() {
wx.vibrateShort();
const manager = wx.getBackgroundAudioManager();
// 如果正在播放,则进行暂停逻辑
if(this.data.playing) {
this.setData({playing:false});
manager.pause();
return;
}
// 如果是暂停,则进行播放逻辑
if(!this.data.playing && !this.data.stoped) {
this.setData({playing:true});
manager.play();
return;
}
// 如果是关闭过,则重放此音乐
if(!this.data.playing && this.data.stoped) {
this.setData({playing:true,stoped:false});
manager.protocol = 'http';
manager.title = this.data.item.name;
manager.epname = this.data.item.name;
manager.singer = 'ASMR';
manager.coverImgUrl = this.data.item.logo;
manager.src = this.data.item.url;
return;
}
},
// 进度跳转
handleSeek(e) {
wx.vibrateShort();
const query = wx.createSelectorQuery();
query.select('#process').boundingClientRect(rect=>{
const manager = wx.getBackgroundAudioManager();
// 如果当前已经停止音乐了,则重新开始播放音乐后,再进行跳转
if(!this.data.playing && this.data.stoped) {
this.setData({playing:true,stoped:false});
manager.protocol = 'http';
manager.title = this.data.item.name;
manager.epname = this.data.item.name;
manager.singer = 'ASMR';
manager.coverImgUrl = this.data.item.logo;
manager.src = this.data.item.url;
}
// 进度栏基本长度和用户点击到的位置长度
const width = rect.width;
const userWidth = e.detail.x - e.target.offsetLeft;
// 计算所在位置百分比
const rate = userWidth/width;
const seek = this.data.totalTime * rate;
manager.seek(seek);
}).exec();
},
// 加载音乐
loadMusic(index) {
if(this.data.items[index]) {
setTimeout(()=>{
this.setData({item:this.data.items[index],playing:true,stoped:false,currentTime:0,totalTime:0},()=>{
// 初始化音乐播放器
const manager = wx.getBackgroundAudioManager();
manager.protocol = 'http';
manager.title = this.data.item.name;
manager.epname = this.data.item.name;
manager.singer = 'ASMR';
manager.coverImgUrl = this.data.item.logo;
manager.src = this.data.item.url;
// 监听主动停止音乐事件
manager.onStop(()=>{
this.setData({playing:false,currentTime:0,stoped:true});
});
// 播放自然停止事件
manager.onEnded(()=>{
this.setData({playing:false,stoped:true,currentTime:this.data.totalTime});
});
// 监听背景播放进度事件
manager.onTimeUpdate(()=>{
this.setData({currentTime:manager.currentTime});
});
// 初始化总时长信息
this.setData({totalTime:this.data.item.time});
});
},500);
}
},
// 上一个
handleLast() {
if(this.data.current > 0){
wx.vibrateShort();
this.setData({current:this.data.current - 1});
}
},
// 下一个
handleNext() {
if(this.data.current < (this.data.items.length - 1)){
wx.vibrateShort();
this.setData({current:this.data.current + 1});
}
},
// 开启分享
onShareAppMessage() {}
})
基于微信小程序云开发的音乐播放器小程序源码(毕业设计).zip
版权申诉
5星 · 超过95%的资源 22 浏览量
2023-02-20
10:36:09
上传
评论 27
收藏 4.54MB ZIP 举报
不安分的小女孩
- 粉丝: 9163
- 资源: 1966
最新资源
- 基于stm32f103c单片机+MPU6050+0.96英寸OLED显示屏双柄遥控器硬件(原理图+PCB)工程文件.zip
- 整理的关于少儿编程的学习路径,以及如何在小升初,初升高和大学充分的利用起来编程经验的优势
- 足球比赛结果统计表2006-2011年大约28W场比赛
- 基于PHP+mysql的社区交流系统(源代码)
- yolov5,SSD 可能使用到的一些代码
- 一键批量生成多层次文件夹结构,使用Python脚本实现嵌套文件夹批量生成
- 基于c51单片机+DS1302+DHT11温湿度模块+LCD1602显示的万年历硬件原理图+BOM+软件程源码序+仿真图.zip
- NSGA2的MATLAB代码
- Messagepassingtest_GCN_DGL.py
- Sh,Docker 运维好帮手,一招通过 sh 脚本批量快速启动和重启多个Docker 容器
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页