123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- const fillIn = val => `${val < 10 ? '0' : ''}${val}`,
- formatTime = _time => {
- let time = Math.round(_time);
- let second = Math.round(time % 60),
- minute = Math.floor(time / 60 % 60),
- hour = Math.floor(time / 60 / 60);
- return `${fillIn(hour)}:${fillIn(minute)}:${fillIn(second)}`;
- };
- class Audio{
- constructor(obj){
- const _ts = this,
- option = _ts.option = obj.attrs;
- _ts.loop = option.loop === 'true',
- _ts.autoplay = option.autoplay === 'true';
- _ts.create();
- _ts.index = 0;
-
- }
- create(){
- const _ts = this,
- option = _ts.option;
- let audio = _ts.audio = wx.createInnerAudioContext();
- audio.src = option.src;
- // 说明可以播放了
- audio.onCanplay(function(){
- if(_ts.autoplay && !_ts.index){
- _ts.play();
- };
- if(!_ts.autoplay && !_ts.index){
- _ts.eventCanplay();
- };
- });
- // 更新时间
- audio.onTimeUpdate(function(){
- //_ts.status = 'update';
- _ts.duration = audio.duration;
- _ts.currentTime = audio.currentTime;
- // 定义播放结束
- if(_ts.duration - _ts.currentTime < 0.5){
- _ts.index++;
- if(_ts.loop){
- audio.stop();
- }else{
- _ts.stop();
- };
- audio.seek(0);
- };
- _ts.eventTimeUpdate(formatTime(_ts.duration),formatTime(_ts.currentTime));
- });
- //
- audio.onSeeked(function(){
- if(_ts.loop){
- _ts.play();
- };
- });
- }
- // 播放
- play(){
- const _ts = this;
- _ts.status = 'play';
- _ts.audio.play();
- _ts.eventPlay();
- }
- // 暂停
- pause(){
- const _ts = this;
- _ts.status = 'pause';
- _ts.audio.pause();
- _ts.eventPause();
- }
- // 停止
- stop(){
- const _ts = this;
- _ts.status = 'stop';
- _ts.audio.stop();
- _ts.eventStop();
- }
- // 销毁
- destroy(){
- const _ts = this;
- _ts.stop();
- _ts.audio.destroy();
- }
- eventCanplay(){}
- eventTimeUpdate(){}
- eventEnded(){}
- eventError(){}
- eventPause(){}
- eventPlay(){}
- eventSeeked(){}
- eventSeeking(){}
- eventStop(){}
- eventWaiting(){}
- };
- module.exports = Audio;
|