pi-rfid-jukebox/jukebox/media-player.js

54 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
const throttle = require('throttle-debounce/throttle');
const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
const DEFAULT_PAUSE_THROTTLE = 2000;
const DEFAULT_PLAY_THROTTLE = 5000;
class MediaPlayer extends ChildProcessEmitter {
constructor(config, logger) {
super(config.mpg321, logger);
this.stderrFilters.push(line => {
return line.substr(0, 3) == '@F '
});
this.pauseThrottled = throttle(config.pause_throttle || DEFAULT_PAUSE_THROTTLE, () => {
this._pause();
});
this.playFileThrottled = throttle(config.play_throttle || DEFAULT_PLAY_THROTTLE, (path) => {
this._playFile(path);
});
// default to unthrottled
this.pause = this._pauseFile;
this.playFile = this._playFile;
}
throttle() {
this.pause = this.pauseThrottled;
this.playFile = this.playFileThrottled;
}
stop() {
this.send('STOP');
}
_pause() {
this.send('PAUSE');
}
_playFile(path) {
this.send("LOAD " + path);
}
}
2017-04-09 17:55:11 +00:00
module.exports = function(config, logger) {
return new MediaPlayer(config, logger);
};
// vim:set ts=2 sw=2 et: