From e10528397f99e803ffc38de64f55f6713c82fdee Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 13 Mar 2018 11:26:29 +0800 Subject: [PATCH] replace dirty checking with timeupdate --- src/js/controller.js | 4 ++-- src/js/player.js | 13 +++++++++++-- src/js/timer.js | 16 +--------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/js/controller.js b/src/js/controller.js index 49ccdb80..7a1fd21f 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -62,11 +62,11 @@ class Controller { percentage = Math.min(percentage, 1); this.player.bar.set('played', percentage, 'width'); this.player.seek(this.player.bar.get('played', 'width') * this.player.audio.duration); - this.player.timer.enable('progress'); + this.player.disableTimeupdate = true; }; this.player.template.barWrap.addEventListener(utils.nameMap.dragStart, () => { - this.player.timer.disable('progress'); + this.player.disableTimeupdate = false; document.addEventListener(utils.nameMap.dragMove, thumbMove); document.addEventListener(utils.nameMap.dragEnd, thumbUp); }); diff --git a/src/js/player.js b/src/js/player.js index 84ffb88a..1726b3b4 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -121,6 +121,17 @@ class APlayer { } }); + this.on('timeupdate', () => { + if (!this.disableTimeupdate) { + this.bar.set('played', this.audio.currentTime / this.audio.duration, 'width'); + this.lrc && this.lrc.update(); + const currentTime = utils.secondToTime(this.audio.currentTime); + if (this.template.ptime.innerHTML !== currentTime) { + this.template.ptime.innerHTML = currentTime; + } + } + }); + // show audio time: the metadata has loaded or changed this.on('durationchange', () => { if (this.audio.duration !== 1) { // compatibility: Android browsers will output 1 at first @@ -268,7 +279,6 @@ class APlayer { }); this.timer.enable('loading'); - this.timer.enable('progress'); if (this.options.mutex) { for (let i = 0; i < instances.length; i++) { @@ -298,7 +308,6 @@ class APlayer { this.audio.pause(); this.timer.disable('loading'); - this.timer.disable('progress'); }); } diff --git a/src/js/timer.js b/src/js/timer.js index cbcdc85d..fb912199 100644 --- a/src/js/timer.js +++ b/src/js/timer.js @@ -1,4 +1,3 @@ -import utils from './utils'; class Timer { constructor (player) { @@ -15,7 +14,7 @@ class Timer { } )(); - this.types = ['loading', 'progress']; + this.types = ['loading']; this.init(); } @@ -27,19 +26,6 @@ class Timer { }); } - initprogressChecker () { - this.progressChecker = setInterval(() => { - if (this.enableprogressChecker) { - this.player.bar.set('played', this.player.audio.currentTime / this.player.audio.duration, 'width'); - this.player.lrc && this.player.lrc.update(); - const currentTime = utils.secondToTime(this.player.audio.currentTime); - if (this.player.template.ptime.innerHTML !== currentTime) { - this.player.template.ptime.innerHTML = currentTime; - } - } - }, 100); - } - initloadingChecker () { let lastPlayPos = 0; let currentPlayPos = 0;