123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- 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
- }
|