var Audio = { //音乐列表 list: [], //音频管理器 manage: false, //当前播放第几首 current: 0, //是否正在播放 running: false, //是否绑定播放进度更新事件 bindProcess: false, //绑定的主事件 obj:false, //最大秒数 max:0, loading:false, init: function (obj) { if (this.manage) { return } if (obj) { this.obj = obj } this.setRunning(false) var audio = wx.getStorageSync('audioaudo') if (!audio) { audio = wx.getBackgroundAudioManager() wx.setStorageSync('audio', audio) } this.manage = audio this.manage.stop() } , add: function (data) { this.list.push(data) } , play: function (index) { this.init() if (!index) { index = 0 } if (this.loading == false) { this.current = index var data = this.list[index] this.setManageAttr(data) } else { this.manage.play() } this.setRunning(true) return this.running } , pause: function () { this.init() this.manage.pause() this.setRunning(false) } , stop: function () { this.init() this.manage.stop() this.setRunning(false) } , setRunning: function (value) { this.running = value wx.setStorageSync('audoRunning', this.running) if (this.obj) { this.obj.setData({ audioRunning: this.running }) } } , getManage: function () { this.init() return this.manage } //跳转播放到多少秒 , seek: function (value) { this.init() this.manage.seek(this.duration() * value) } //根据百分比值,计算总秒数 , duration: function () { this.init() return this.manage.duration / 100 } //根据当前秒数,计算当前进度 , currentTime: function () { this.init() if (this.manage.currentTime >= this.manage.duration) { return 100 } return (this.manage.currentTime / this.manage.duration) * 100 } //播放进度更新 , process: function () { this.init() if (!this.obj) { return } var that = this this.manage.onTimeUpdate(function () { var cur = that.currentTime() if (cur >= 100) { cur = 0 that.running = false } if (that.max <= 0) { that.max = that.manage.duration } that.obj.setData({ audioProcess: cur, audioCurTime: that.time(that.manage.currentTime), audioTotalTime: that.time(that.max) }) }) this.bindProcess = true } , setManageAttr : function (data) { this.loading = true this.manage.title = data.title this.manage.epname = data.epname this.manage.singer = data.singer this.manage.coverImgUrl = data.coverImgUrl this.manage.src = data.src this.setOn() } , time : function(value) { var secondTime = parseInt(value);// 秒 var minuteTime = 0;// 分 var hourTime = 0;// 小时 if (secondTime > 60) {//如果秒数大于60,将秒数转换成整数 //获取分钟,除以60取整数,得到整数分钟 minuteTime = parseInt(secondTime / 60); //获取秒数,秒数取佘,得到整数秒数 secondTime = parseInt(secondTime % 60); } if (secondTime < 10) { secondTime = '0' + secondTime } var result = "" + secondTime + ""; if (minuteTime > 0) { if (minuteTime < 10) { minuteTime = '0' + minuteTime } result = "" + minuteTime + ":" + result; } else { result = "00:" + result; } return result } , setOn : function () { var that = this this.manage.onPause(function () { that.setRunning(false) }) this.manage.onStop(function () { that.loading = false that.setRunning(false) }) this.manage.onPlay(function () { that.loading = true that.setRunning(true) }) this.manage.onEnded(function () { that.loading = false that.setRunning(false) }) } } module.exports = { load: Audio }