变量 | 说明 |
---|---|
baseUrl | 服务器地址,便于切换数据源 |
lists | 播放列表;节点信息参考如下述代码 |
ind | 播放列表索引 |
isPause | 播放标记;播放状态 |
per | 进度条;百分比 |
cur | 当前时间;00:00 |
dur | 播放时长;00:00 |
{ "id": 5, "title": "微风细雨", "singer": "wang", "src": "/music/wang.mp3", "coverImgUrl": "/music/wangfei.png" }
const BGM = wx.getBackgroundAudioManager(); const timeFormat = (num) => { let min = parseInt(num / 60); let sec = parseInt(num) % 60 return `${to2Digit(min)}:${to2Digit(sec)}` }; const to2Digit = (num) => { return num > 9 ? num : '0' + num; };
data: { lists: [], ind: 0, cur: 0, dur: 0, per: 0, isPause: true. baseUrl: 'https://glpla.github.io/utils' }
onLoad() { wx.showLoading({ title: 'loading', }) wx.request({ url: this.data.baseUrl + '/data/song_list.json', success: res => { this.setData({ lists: res.data.cont }) this.init(this.data.ind) } }) }
playBgm() { BGM.play() }, pauseBgm() { BGM.pause() }
init(ind) { BGM.title = this.data.lists[ind].title BGM.singer = this.data.lists[ind].singer BGM.src = this.data.baseUrl + this.data.lists[ind].src BGM.onCanplay(() => { console.log('music is ready') wx.hideLoading() }) BGM.onPlay(() => { console.log('play', BGM.duration); this.setData({ isPause: false }) }) BGM.onPause(() => { console.log('pause'); this.setData({ isPause: true }) }) BGM.onEnded(() => { console.log('ended'); this.setData({ isPause: true, cur:'00:00', dur:'00:00' }) }) BGM.onError(() => { console.log('error'); }) BGM.onTimeUpdate(() => { console.log('time updating'); this.setData({ cur: BGM.currentTime per: parseInt(BGM.currentTime / BGM.duration * 100) }) }) }
prevSong() { let ind = this.data.ind if (ind > 0) { ind-- } this.setData({ ind }) this.init(ind) }, nextSong() { let ind = this.data.ind if (ind < this.data.lists.length - 1) { ind++ } this.setData({ ind }) this.init(ind) }
sliderChange(e) { let per = parseInt(e.detail.value * BGM.duration / 100) BGM.seek(per) }
const util = require('../../utils/minsec.js') const BGM = getApp().globalData.BGM Page({ data: { lists: [], ind: 0, cur: '00:00', dur: '00:00', per: 0, isPause: true, isLike: false, isLoop: false, isList: false, baseUrl: 'https://glpla.github.io/utils' }, onLoad(optons) { console.log(BGM); wx.showLoading({ title: 'Loading', mask: true }) wx.request({ url: this.data.baseUrl + '/data/song_list.json', success: res => { this.setData({ lists: res.data.cont }) this.init(this.data.ind) } }) }, playBGM() { BGM.play() }, pauseBGM() { BGM.pause() }, sliderChange(e) { let per = parseInt(e.detail.value * BGM.duration / 100) console.log(e.detail.value, per); BGM.seek(per) }, prevSong() { let ind = this.data.ind if (ind > 0) { ind-- } else { if (this.data.isLoop) { ind = this.data.lists.length - 1 } else { return; } } this.setData({ ind: ind }) this.init(ind) }, nextSong() { let ind = this.data.ind if (ind < this.data.lists.length - 1) { ind++ } else { if (this.data.isLoop) { ind = 0 } else { return } } console.log('ind', ind); this.setData({ ind: ind }) this.init(ind) }, init(ind) { this.setData({ isPause: true }) wx.showLoading({ title: 'Buffering', }) BGM.src = this.data.baseUrl + this.data.lists[ind].src; BGM.title = this.data.lists[ind].title; BGM.singer = this.data.lists[ind].singer; BGM.onCanplay(() => { console.log('music is ready') wx.hideLoading() }) BGM.onPlay(() => { console.log('bgm play', BGM.duration) this.setData({ dur: util.minsec(BGM.duration), isPause: false }) wx.hideToast() }) BGM.onPause(() => { console.log('bgm pause') this.setData({ isPause: true }) }) BGM.onWaiting(() => { console.log('waiting'); wx.showToast({ title: 'Buffering', }) }) BGM.onError(() => { console.log('bgm error') wx.hideLoading() wx.showToast({ title: 'Try later', }) }) BGM.onEnded(() => { console.log('bgm ended'); if (this.data.isLoop) { this.nextSong() } else { this.init(this.data.ind) } }) BGM.onTimeUpdate(() => { this.setData({ cur: util.minsec(BGM.currentTime), per: parseInt(BGM.currentTime / BGM.duration * 100) }) }) }, setSong(e) { let ind = e.currentTarget.dataset.ind console.log(ind); this.setData({ ind }) this.init(ind) }, setLike() { let isLike = !this.data.isLike this.setData({ isLike }) }, setLoop() { let isLoop = !this.data.isLoop this.setData({ isLoop }) }, showList() { let isList = !this.data.isList this.setData({ isList }) }, onShareAppMessage() { return { title: 'Music from glpla', path: '/page/music/music' } } })