-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
56 lines (47 loc) · 1.08 KB
/
player.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
const { spawn } = require("child_process");
const loudness = require("loudness");
class Player {
childProcess = null;
constructor(streamURL, log) {
this.streamURL = streamURL;
this.playing = false;
this.lastVolume = 1;
this.log = log
}
isPlaying() {
return this.playing;
}
async play() {
if (!this.playing) {
this.volume = 20;
this.playing = true;
await loudness.setVolume(this.volume);
this.log.info(`Playing stream ${this.streamURL}`)
this.childProcess = spawn("mpg123", [this.streamURL]);
}
}
stop() {
if (this.childProcess) {
this.childProcess.kill();
this.log.info("Child process stopped.");
this.playing = false;
} else {
this.log.warn("No child process running.");
this.playing = false;
}
}
async setVolume(value) {
this.lastVolume = value;
this.volume = value;
await loudness.setVolume(100 * value);
}
getVolume() {
return this.lastVolume;
}
mute() {
this.lastVolume = 0;
this.volume = 0;
this.stop();
}
}
module.exports = Player;