From 5e743aec52ad22ab36ba1dbcca68ddc1cad14240 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 22 Dec 2018 14:21:53 -0400 Subject: Progress info, feedback on stop, etc --- players.js | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/players.js b/players.js index ba2cd29..dccdea2 100644 --- a/players.js +++ b/players.js @@ -74,32 +74,55 @@ module.exports.WebPlayer = class extends Player { constructor() { super() - this.audioEl = document.createElement('audio') + const secToMore = time => ({ + hour: Math.floor(time / 3600), + min: Math.floor((time % 3600) / 60), + sec: Math.floor(time % 60) + }) + + setInterval(() => { + if (!this.audioEl) return + + const { hour: curHour, min: curMin, sec: curSec } = secToMore(this.audioEl.currentTime) + const { hour: lenHour, min: lenMin, sec: lenSec } = secToMore(this.audioEl.duration) + + this.printStatusLine(getTimeStrings({ + curHour, curMin, curSec, + lenHour, lenMin, lenSec + })) + }, 50) } playFile(file) { + this.audioEl = document.createElement('audio') this.audioEl.src = file this.audioEl.play() - return new Promise(resolve => { - const handleEnded = () => { - this.audioEl.removeEventListener('ended', handleEnded) - resolve() - } + return Promise.race([ + new Promise(resolve => this.stopPromise = resolve), + new Promise(resolve => { + const handleEnded = () => { + this.audioEl.removeEventListener('ended', handleEnded) + resolve() + } - this.audioEl.addEventListener('ended', handleEnded) - }) + this.audioEl.addEventListener('ended', handleEnded) + }) + ]) } seekAhead(secs) { + if (!this.audioEl) return this.audioEl.currentTime += secs } seekBack(secs) { + if (!this.audioEl) return this.audioEl.currentTime -= secs } togglePause() { + if (!this.audioEl) return if (this.audioEl.paused) { this.audioEl.play() } else { @@ -108,8 +131,13 @@ module.exports.WebPlayer = class extends Player { } kill() { + if (!this.audioEl) return this.audioEl.currentTime = 0 this.audioEl.pause() + if (this.stopPromise) { + this.stopPromise() + } + delete this.audioEl } } -- cgit 1.3.0-6-gf8a5