« get me outta code hell

Progress info, feedback on stop, etc - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-22 14:21:53 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-22 14:21:53 -0400
commit5e743aec52ad22ab36ba1dbcca68ddc1cad14240 (patch)
tree2302eaeb618c7200113e8979d986450e84516c81
parent62fe616bc831f2be61743f054e38d5e8ae62c202 (diff)
Progress info, feedback on stop, etc
-rw-r--r--players.js44
1 files 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
   }
 }