-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMediaPlayer.js
33 lines (27 loc) · 916 Bytes
/
MediaPlayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"use strict";
var exec = require('child_process').exec;
function MediaPlayer(){
this.isPlaying = false;
this.currentMediaPath = null;
this.playingCompleteCallback = null;
}
MediaPlayer.prototype.play = function(path, playingCompleteCallback){
if (!this.isPlaying){
this.playingCompleteCallback = playingCompleteCallback;
this.isPlaying = true;
var command = "mplayer -novideo '" + path + "'";
exec(command, {maxBuffer: 1024 * 1024}, this.songComplete.bind(this));
}
};
MediaPlayer.prototype.songComplete = function(err, stdout, stderr) {
this.isPlaying = false;
this.playingCompleteCallback();
};
MediaPlayer.prototype.stop = function() {
if (this.isPlaying) {
this.isPlaying = false;
// Executing the following exec will execute 'songComplete' callback
exec("killall mplayer");
}
};
module.exports = new MediaPlayer;