« get me outta code hell

(l) to loop currently playing track - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/players.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-01-24 17:09:25 -0400
committerFlorrie <towerofnix@gmail.com>2019-01-24 17:09:25 -0400
commitb4e575f4ec8b65464f9a8f109ddf3907cf30b853 (patch)
treeaa1c68ba24a8da7f07770464f8d2505c9102b822 /players.js
parent3c04268c5e3ee34b6aaf3d7d6e21316741132ff0 (diff)
(l) to loop currently playing track
Diffstat (limited to 'players.js')
-rw-r--r--players.js28
1 files changed, 27 insertions, 1 deletions
diff --git a/players.js b/players.js
index be5205f..fdcd038 100644
--- a/players.js
+++ b/players.js
@@ -49,6 +49,7 @@ class Player extends EventEmitter {
     super()
 
     this.disablePlaybackStatus = false
+    this.isLooping = false
   }
 
   set process(newProcess) {
@@ -72,6 +73,7 @@ class Player extends EventEmitter {
   volUp(amount) {}
   volDown(amount) {}
   togglePause() {}
+  toggleLoop() {}
 
   async kill() {
     if (this.process) {
@@ -92,7 +94,11 @@ class Player extends EventEmitter {
 
 module.exports.MPVPlayer = class extends Player {
   getMPVOptions(file) {
-    return ['--no-video', file]
+    const opts = ['--no-video', file]
+    if (this.isLooping) {
+      opts.unshift('--loop')
+    }
+    return opts
   }
 
   playFile(file) {
@@ -101,6 +107,8 @@ module.exports.MPVPlayer = class extends Player {
 
     this.process = spawn('mpv', this.getMPVOptions(file))
 
+    let lastPercent = 0
+
     this.process.stderr.on('data', data => {
       if (this.disablePlaybackStatus) {
         return
@@ -117,6 +125,19 @@ module.exports.MPVPlayer = class extends Player {
           percent // ###%
         ] = match.slice(1)
 
+        if (lastPercent < parseInt(percent)) {
+          lastPercent = parseInt(percent)
+          // mpv forgets commands you sent it whenever it loops, so you
+          // have to specify them every time it loops. We do that whenever the
+          // position in the song decreases, since that means it may have
+          // looped.
+          if (this.isLooping) {
+            this.sendCommand('set loop yes')
+          } else {
+            this.sendCommand('set loop no')
+          }
+        }
+
         this.printStatusLine(getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}))
       }
     })
@@ -164,6 +185,11 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer {
     this.sendCommand('cycle pause')
   }
 
+  toggleLoop() {
+    this.isLooping = !this.isLooping
+    this.sendCommand('cycle loop')
+  }
+
   kill() {
     if (this.fifo) {
       this.fifo.close()