-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmplayer-manager.js
141 lines (129 loc) · 3.22 KB
/
mplayer-manager.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";
const { spawn } = require('child_process');
const EventEmitter = require('events');
const util = require('util');
const path = require('path');
const STOPPED = "stopped";
const LOADING = "loading...";
const PLAYING = "playing";
module.exports = class mPlayerManager extends EventEmitter {
emitStatus() {
this.emit('status', this.nowPlaying);
}
constructor() {
super();
this.url = require(path.resolve('./').concat('/radioStreams.json'));
this.options = {
app: '/usr/bin/mplayer',
cli: ['-slave']
};
this.runtime = '';
this.currentlyPlaying = "";
this.nowPlaying = {
status: STOPPED,
url: {},
statusList: {
STOPPED: STOPPED,
LOADING: LOADING,
PLAYING: PLAYING
}
};
this.mp = {};
this.killCB = false;
this.loop = false;
this.intStatus = setInterval(() => {
this.emitStatus();
}, 1000);
};
checkStatus(cb) {
if (this.mpIsRunning()) {
this.killCB = cb;
return this.killPlayer();
}
cb();
}
mpIsRunning() {
return (this.mp && this.mp.hasOwnProperty('pid')) ? true : false;
}
mpCommand(cmd) {
if (!this.mpIsRunning()) return;
this.mp.stdin.write(cmd.concat("\n"));
}
killPlayer() {
this.mpCommand('quit');
}
setVolume(val, abs) {
if (!this.mpIsRunning()) return;
if (abs) return this.mpCommand(`volume ${val} 1`);
return this.mpCommand(`volume ${val}`);
}
mpNewPlayer(url) {
this.mp = spawn(this.options.app, this.options.cli.concat(url.url));
this.loop = false;
this.nowPlaying.status = LOADING;
this.nowPlaying.url = Object.assign({}, url);
this.mp.stdout.on('data', data => this.mpStdout(data));
this.mp.stderr.on('data', data => this.mpStderr(data));
this.mp.on('close', () => this.mpClose());
}
mpStdout(data) {
const tmp = data.toString('utf-8').trim();
if (tmp.substr(0, 2) === 'A:') {
this.currentlyPlaying = tmp;
if (this.nowPlaying.status !== PLAYING) this.nowPlaying.status = PLAYING;
if (!this.loop) {
this.loop = true;
setTimeout(() => {
this.mpCommand('loop 1');
}, 4000);
}
}else{
this.runtime = this.runtime.concat(tmp);
if (this.intRT) clearTimeout(this.intRT);
this.intRT = setTimeout(() => {
console.log(this.runtime);
this.runtime = '';
}, 2000);
}
// console.log(this.currentlyPlaying);
}
mpStderr(data) {
console.log('error', data.toString());
}
mpClose() {
console.log('fin');
this.nowPlaying.status = STOPPED;
this.resetMp();
}
resetMp() {
this.mp.stdout.removeAllListeners('data');
this.mp.stderr.removeAllListeners('data');
this.mp.removeAllListeners('close');
this.mp = {};
if (typeof this.killCB === "function") return this.killCB();
}
playStream(url) {
this.checkStatus(() => {
this.mpNewPlayer(url);
this.killCB = false;
});
};
stopStream(url) {
this.killPlayer();
}
volumeUp() {
this.setVolume(1);
}
volumeDown() {
this.setVolume(-1);
}
volume(val) {
this.setVolume(Number(val), true);
}
getStatus() {
this.mpCommand("get_time_pos");
}
getUrl() {
return this.url;
};
};