Skip to content

Commit

Permalink
syncProgressTimer() after seekTo event
Browse files Browse the repository at this point in the history
  • Loading branch information
rtshkmr committed Feb 24, 2024
1 parent aecfad8 commit 77b4896
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions assets/js/hooks/audio_player.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ AudioPlayer = {
this.nextTimer = null
},
clearProgressTimer() {
this.updateProgress()
console.log("[clearProgressTimer]", {
timer: this.progressTimer,
})
Expand All @@ -109,30 +110,21 @@ AudioPlayer = {
this.player.currentTime = currentTime;
this.currentTime.innerText = this.formatTime(currentTime);
}
const progressUpdateInterval = 100 // 10fps, comfortable for human eye

if (!this.progressTimer) { // single instance of progress timer
this.progressTimer = setInterval(() => this.updateProgress(), progressUpdateInterval)
}
console.log("[play: ProgressTimer]: ", this.progressTimer)

this.syncProgressTimer()
}, error => {
if(error.name === "NotAllowedError"){
execJS("#enable-audio", "data-js-show")
}
})
},

pause(){
this.clearProgressTimer()
this.player.pause()
},

stop(){
this.clearProgressTimer()
this.player.pause()
this.player.currentTime = 0
this.updateProgress()
this.clearProgressTimer()
this.duration.innerText = ""
this.currentTime.innerText = ""
},
Expand All @@ -145,7 +137,23 @@ AudioPlayer = {
this.playbackBeganAt = beginTime;
this.currentTime.innerText = this.formatTime(positionS);
this.player.currentTime = positionS;
this.updateProgress()
this.syncProgressTimer()
},

/**
* Calls the update progress fn at a set interval,
* replaces an existing progress timer, if it exists.
* */
syncProgressTimer() {
const progressUpdateInterval = 100 // 10fps, comfortable for human eye
const hasExistingTimer = this.progressTimer
if(hasExistingTimer) {
this.clearProgressTimer()
}
if (this.player.paused) {
return
}
this.progressTimer = setInterval(() => this.updateProgress(), progressUpdateInterval)
},
/**
* Updates playback progress information.
Expand Down Expand Up @@ -202,7 +210,8 @@ AudioPlayer = {
}

const currentTimeMs = currentTime * 1000
const activeEvent = events.find(event => currentTimeMs >= event.origin && currentTimeMs < (event.origin + event.duration))
const activeEvent = events.find(event => currentTimeMs >= event.origin &&
currentTimeMs < (event.origin + event.duration))
console.log("activeEvent:", {currentTimeMs, activeEvent})

if (!activeEvent) {
Expand Down

0 comments on commit 77b4896

Please sign in to comment.