-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
83 lines (67 loc) · 1.87 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const vlc = require("@richienb/vlc")
class Player {
constructor(src) {
this.playing = false
this.volume = 1
this.currentTime = 0
this.src = src
this.setup = (async () => {
this.player = await vlc()
if (src) {
await this.player.command("in_enqueue", {
input: src
})
}
this.timeUpdater = setInterval(async () => {
const { length: duration, time: currentTime } = await this.player.info();
this.duration = duration;
this.currentTime = currentTime;
// if (duration === 0 && currentTime === 0) {
// this.playing = false;
// }
}, 1000);
})();
}
async play() {
if (!this.playing) {
this.playing = true;
await this.setup;
await this.player.command("pl_pause", {
id: 0
});
}
}
async pause() {
if (this.playing) {
this.playing = false;
await this.setup;
await this.player.command("pl_pause", {
id: 0
});
}
}
async next(value) {
this.src = value;
await this.setup;
await this.player.command("pl_empty");
await this.player.command("in_play", {
input: value
});
this.player.info().then(_ => {
this.play()
})
}
async setVolume(value) {
if (value>200 || value<0)
return
value = value/100
await this.setup;
this.volume = value;
await this.player.command("volume", { val: Math.round(value * 256) });
}
destroy() {
clearInterval(this.timeUpdater);
this.player.kill();
}
}
module.exports = Player