-
Notifications
You must be signed in to change notification settings - Fork 5
/
player.js
61 lines (49 loc) · 1.27 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
57
58
59
60
61
const request = require('request'),
lame = require('lame'),
Speaker = require('speaker'),
fs = require('fs'),
Volume = require("pcm-volume");
// const streamURL = 'https://rbb-radioeins-live.sslcast.addradio.de/rbb/radioeins/live/mp3/128/stream.mp3';
class Player {
constructor(streamURL) {
this.streamURL = streamURL;
this.playing = false;
this.lastVolume = 1;
}
isPlaying() {
return this.playing;
}
play() {
if (!this.playing) {
this.volume = new Volume();
this.volume.setVolume(this.lastVolume);
this.stream = request(this.streamURL);
this.stream.pipe(new lame.Decoder())
.pipe(this.volume)
.pipe(new Speaker());
this.playing = true;
}
}
stop() {
if (this.playing) {
this.stream.abort();
this.playing = false;
}
}
setVolume(value) {
this.lastVolume = value;
if(this.volume) {
this.volume.setVolume(value);
}
}
getVolume() {
return this.lastVolume;
}
mute() {
this.lastVolume = 0;
if(this.volume) {
this.volume.setVolume(0);
}
}
}
module.exports = Player;