Skip to content

Commit

Permalink
replace dirty checking with timeupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Mar 13, 2018
1 parent 6deeacd commit e105283
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/js/controller.js
Expand Up @@ -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);
});
Expand Down
13 changes: 11 additions & 2 deletions src/js/player.js
Expand Up @@ -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
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -298,7 +308,6 @@ class APlayer {
this.audio.pause();

this.timer.disable('loading');
this.timer.disable('progress');
});
}

Expand Down
16 changes: 1 addition & 15 deletions src/js/timer.js
@@ -1,4 +1,3 @@
import utils from './utils';

class Timer {
constructor (player) {
Expand All @@ -15,7 +14,7 @@ class Timer {
}
)();

this.types = ['loading', 'progress'];
this.types = ['loading'];

this.init();
}
Expand All @@ -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;
Expand Down

0 comments on commit e105283

Please sign in to comment.