(function (window, document) { var wxaudio = function (options) { if (!(this instanceof wxaudio)) return new wxaudio(options) this.value = { ele: '', width: '320px', title: '语音播报', src: '', disc: '', loop: true } this.opt = this.extend(this.value, options, true) // 判断传进来的是dom还是字符串 if ((typeof options.ele) === "string") { this.opt.ele = document.queryselector(options.ele) }else{ this.opt.ele = options.ele } this.loading = false this.isdrag = false this.isplaying = false this.durationt = 0 this.currentt = 0 this.currentp = 0 this.maxprogresswidth = 0 this.dragprogressto = 0 // 通过时间戳与当前时间的差值来判断是否需要加载 this.reducetbefore = 0 // 时间戳与当前时间的差值 (初始化) this.reducetafter = 0 // 时间戳与当前时间的差值 (执行中) this.initdom(); } wxaudio.prototype = { constructor: this, initdom: function () { // content this.wxaudioc = document.createelement('div') this.wxaudioc.classname = 'wx-audio-content' this.wxaudioc.style.width = this.opt.width this.opt.ele.appendchild(this.wxaudioc) // audio this.wxaudio = document.createelement('audio') this.wxaudio.classname = 'wx-audio-content' this.wxaudio.src = this.opt.src // this.wxaudio.setattribute("preload","auto") // this.wxaudio.preload = 'auto' this.wxaudioc.appendchild(this.wxaudio) // left this.wxaudiol = document.createelement('div') this.wxaudiol.classname = 'wx-audio-left' this.wxaudioc.appendchild(this.wxaudiol) // left image this.wxaudiostateimg = document.createelement('img') this.wxaudiostateimg.classname = 'wx-audio-state' //this.wxaudiostateimg.src = 'http://www.daiwei.org/components/wx-audio/images/pause.png' this.wxaudiostateimg.src = '/zmimages/182091.png' this.wxaudiol.appendchild(this.wxaudiostateimg) // right this.wxaudior = document.createelement('div') this.wxaudior.classname = 'wx-audio-right' this.wxaudioc.appendchild(this.wxaudior) // right title this.wxaudiot = document.createelement('p') this.wxaudiot.classname = 'wx-audio-title' this.wxaudiot.innertext = this.opt.title this.wxaudior.appendchild(this.wxaudiot) // right disc this.wxaudiod = document.createelement('p') this.wxaudiod.classname = 'wx-audio-disc' this.wxaudiod.innertext = this.opt.disc this.wxaudior.appendchild(this.wxaudiod) // right progrees this.wxaudiop = document.createelement('div') this.wxaudiop.classname = 'wx-audio-progrees' this.wxaudior.appendchild(this.wxaudiop) // right progress detail this.wxaudiodetail = document.createelement('div') this.wxaudiodetail.classname = 'wx-progrees-detail' this.wxaudiop.appendchild(this.wxaudiodetail) // voice p this.wxvoicep = document.createelement('span') this.wxvoicep.classname = 'wx-voice-p' this.wxaudiodetail.appendchild(this.wxvoicep) // buffer p this.wxbufferp = document.createelement('span') this.wxbufferp.classname = 'wx-buffer-p' this.wxaudiodetail.appendchild(this.wxbufferp) // loading p this.wxloading = document.createelement('span') this.wxloading.classname = 'wx-loading' this.wxaudiodetail.appendchild(this.wxloading) // laoding wrapper this.wxloadingwrapper = document.createelement('span') this.wxloadingwrapper.classname = 'wx-loading-wrapper' this.wxloading.appendchild(this.wxloadingwrapper) // origin this.wxaudioorigin = document.createelement('div') this.wxaudioorigin.classname = 'wx-audio-origin' this.wxaudiop.appendchild(this.wxaudioorigin) // 音乐时间信息 this.wxaudiotime = document.createelement('div') this.wxaudiotime.classname = 'wx-audio-time' this.wxaudior.appendchild(this.wxaudiotime) // currentt this.wxaudiocurrent = document.createelement('span') this.wxaudiocurrent.classname = 'current-t' this.wxaudiocurrent.innertext = '00:00' this.wxaudiotime.appendchild(this.wxaudiocurrent) // durationt this.wxaudioduration = document.createelement('span') this.wxaudioduration.classname = 'duration-t' this.wxaudioduration.innertext = '00:00' this.wxaudiotime.appendchild(this.wxaudioduration) this.initaudioevent(); }, audioplay: function () { this.wxaudio.play(); this.isplaying = true }, audiopause: function () { this.wxaudio.pause(); this.isplaying = false }, audioplaypause: function () { if (this.isplaying) { this.audiopause(); } else { this.audioplay(); } }, audiocut: function (src, title, disc) { this.wxaudio.src = src this.wxaudiot.innertext = title this.wxaudiod.innertext = disc this.durationt = 0 this.currentt = 0 this.currentp = 0 this.dragprogressto = 0 // 初始化 wxaudiocurrent 的文案 this.wxaudiocurrent.innertext = '00:00' this.wxaudioorigin.style.left = '0px' this.wxvoicep.style.width = '0px' this.audioplay() }, showloading: function (bool) { this.loading = bool || false if (this.loading) { this.wxloading.style.display = 'block' } else { this.wxloading.style.display = 'none' } }, initaudioevent: function () { var _this = this // 音频事件 _this.wxaudio.onplaying = function () { var date = new date () _this.isplaying = true _this.reducetbefore = date.parse(date) - math.floor(_this.wxaudio.currenttime * 1000) //_this.wxaudiostateimg.src = 'http://www.daiwei.org/components/wx-audio/images/playing.gif' _this.wxaudiostateimg.src = '/zmimages/182091.png' }, _this.wxaudio.onpause = function () { _this.isplaying = false _this.showloading(false) //_this.wxaudiostateimg.src = 'http://www.daiwei.org/components/wx-audio/images/pause.png' _this.wxaudiostateimg.src = '/zmimages/182091.png' }, _this.wxaudio.onloadedmetadata = function () { _this.durationt = _this.wxaudio.duration // 初始化视频时间 _this.wxaudioduration.innertext = _this.formarttime(_this.wxaudio.duration) }, _this.wxaudio.onwaiting = function () { if(!_this.wxaudio.paused) { _this.showloading(true) } }, _this.wxaudio.oncanplay = function () { _this.durationt = _this.wxaudio.duration // 初始化视频时间 _this.wxaudioduration.innertext = _this.formarttime(_this.durationt) }, _this.wxaudio.onprogress = function () { if(_this.wxaudio.buffered.length > 0) { var bufferedt = 0 for (var i = 0; i < _this.wxaudio.buffered.length; i++) { bufferedt += _this.wxaudio.buffered.end(i) - _this.wxaudio.buffered.start(i) if(bufferedt > _this.durationt) { bufferedt = _this.durationt _this.showloading(false) //console.log('缓冲完成') } } var bufferedp = math.floor((bufferedt / _this.durationt) * 100) _this.wxbufferp.style.width = bufferedp + '%' } // =========================== var date = new date () // console.log(_this.reducetafter + '-------------------------' + _this.reducetbefore) if(!_this.wxaudio.paused) { _this.reducetafter = date.parse(date) - math.floor(_this.currentt * 1000) if(_this.reducetafter - _this.reducetbefore > 1000) { _this.showloading(true) } else { _this.showloading(false) } } else { return } }, // 绑定进度条 _this.wxaudio.ontimeupdate = function () { var date = new date () if (!_this.isdrag) { _this.currentt = _this.wxaudio.currenttime _this.currentp = number((_this.wxaudio.currenttime / _this.durationt) * 100) _this.reducetbefore = date.parse(date) - math.floor(_this.currentt * 1000) _this.currentp = _this.currentp > 100 ? 100 : _this.currentp _this.wxvoicep.style.width = _this.currentp + '%' _this.wxaudioorigin.style.left = _this.currentp + '%' // 更改时间进度 _this.wxaudiocurrent.innertext = _this.formarttime(_this.wxaudio.currenttime) _this.showloading(false) } }, // 页面点击事件 _this.wxaudiostateimg.onclick = function () { _this.audioplaypause() } _this.wxaudioorigin.onmousedown = function (event) { _this.isdrag = true var e = event || window.event var x = e.clientx var l = event.target.offsetleft // console.log(x) // console.log(l) // console.log(_this.maxprogresswidth) _this.maxprogresswidth = _this.wxaudiodetail.offsetwidth _this.wxaudioc.onmousemove = function (event) { if (_this.isdrag) { var e = event || window.event var thisx = e.clientx _this.dragprogressto = math.min(_this.maxprogresswidth, math.max(0, l + (thisx - x))) // _this.wxaudioorigin.style.left = to + 'px' //console.log(e.clientx + '--------') //console.log(_this.maxprogresswidth + '--------') //console.log(l + (thisx - x) + '--------') // update time _this.updateporgress() } } _this.wxaudioc.onmouseup = function () { //console.log(_this.dragprogressto +' ------- '+ _this.maxprogresswidth + ' ---------- ' + _this.durationt) if (_this.isdrag) { _this.isdrag = false _this.wxaudio.currenttime = math.floor(_this.dragprogressto / _this.maxprogresswidth * _this.durationt) } else { return } } _this.wxaudioc.onmouseleave = function () { if (_this.isdrag) { _this.isdrag = false _this.wxaudio.currenttime = math.floor(_this.dragprogressto / _this.maxprogresswidth * _this.durationt) } else { return } } } _this.wxaudioorigin.ontouchstart = function (event) { _this.isdrag = true var e = event || window.event var x = e.touches[0].clientx var l = e.target.offsetleft _this.maxprogresswidth = _this.wxaudiodetail.offsetwidth _this.wxaudioc.ontouchmove = function (event) { if (_this.isdrag) { var e = event || window.event var thisx = e.touches[0].clientx _this.dragprogressto = math.min(_this.maxprogresswidth, math.max(0, l + (thisx - x))) _this.updateporgress() } }, _this.wxaudioc.ontouchend = function () { // console.log(_this.dragprogressto +' ------- '+ _this.maxprogresswidth + ' ---------- ' + _this.durationt) if (_this.isdrag) { _this.isdrag = false _this.wxaudio.currenttime = math.floor(_this.dragprogressto / _this.maxprogresswidth * _this.durationt) } else { return } } } _this.wxaudiodetail.onclick = function (event) { var e = event || window.event var l = e.layerx var w = _this.wxaudiodetail.offsetwidth // console.log(l + '------------' + w) _this.wxaudio.currenttime = math.floor(l / w * _this.durationt) } // _this.wxaudioorigin.ontachstart = function () { // } }, updateporgress: function () { this.wxaudioorigin.style.left = this.dragprogressto + 'px' this.wxvoicep.style.width = this.dragprogressto + 'px' var currenttime = math.floor(this.dragprogressto / this.maxprogresswidth * this.durationt) // this.wxaudio.currenttime = currenttime this.wxaudiocurrent.innertext = this.formarttime(currenttime) // this.wxaudio.currenttime = math.floor(this.dragprogressto / this.maxprogresswidth * this.durationt) }, formarttime: function (seconds) { var formatnumber = function (n) { n = n.tostring() return n[1] ? n : '0' + n } var m = math.floor(seconds / 60); var s = math.floor(seconds % 60); return formatnumber(m) + ":" + formatnumber(s); }, extend: function(o,n,override) { for(var key in n){ if (n.hasownproperty(key) && (!o.hasownproperty(key) || override)) { o[key] = n[key] } } return o } } // 暴露方法 window.wxaudio = wxaudio; })(window, document)