Skip to content

Instantly share code, notes, and snippets.

@alepez
Last active December 27, 2015 11:49
Show Gist options
  • Save alepez/7320809 to your computer and use it in GitHub Desktop.
Save alepez/7320809 to your computer and use it in GitHub Desktop.
var spawn = require('child_process').spawn;
var util = require('util');
var commands = {
'pause' : 'p',
'quit' : 'q',
'play' : '.', // TODO: check
'forward' : "$'\\x1b\\x5b\\x43'", // TODO: check
'backward' : "$'\\x1b\\x5b\\x44'" // TODO: check
};
var omxProcess = null;
var paused = false;
var sendAction = function(action) {
if (commands[action] && omxProcess) {
omxProcess.stdin.write(commands[action]);
}
};
var open = function(files, options) {
var settings = options || {};
var cmd = 'omxplayer';
var args = [ '-o', 'local' ];
if (settings.loop === true) {
args.push('-L');
}
if (typeof files === 'string') {
args.push(files);
} else if (util.isArray(files)) {
args.push.apply(args, files);
} else {
/* ignora */
}
omxProcess = spawn(cmd, args, {
stdio : [ 'pipe', null, null ]
});
};
var play = function(files, options) {
if (omxProcess) {
if (!paused) {
return;
}
sendAction('pause');
paused = false;
return;
}
if (!files) {
throw new TypeError("No files specified");
}
if (typeof files != 'string' && !util.isArray(files)) {
throw new TypeError("Incorrect value for files: " + files);
}
open(files, options);
};
var pause = function() {
if (paused) {
return;
}
sendAction('pause');
paused = true;
};
var stop = function() {
sendAction('quit');
omxProcess = null;
};
var isPlaying = function() {
return omxProcess && !paused;
};
var isLoaded = function() {
return omxProcess;
};
module.exports = {
play : play,
pause : pause,
stop : stop,
isPlaying : isPlaying,
isLoaded : isLoaded
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment