From b4e575f4ec8b65464f9a8f109ddf3907cf30b853 Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 24 Jan 2019 17:09:25 -0400 Subject: (l) to loop currently playing track --- players.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'players.js') 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() -- cgit 1.3.0-6-gf8a5