2021-04-09 11:23:13 +00:00
|
|
|
module.exports = class AudioQueue {
|
|
|
|
constructor(connection) {
|
|
|
|
this.connection = connection;
|
|
|
|
this.queue = [];
|
|
|
|
this.current = undefined;
|
|
|
|
}
|
|
|
|
playNext() {
|
|
|
|
if (this.queue.length == 0) {
|
|
|
|
this.current = undefined;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.current = this.connection.play(this.queue[0]);
|
|
|
|
this.current.on('speaking', (val) => {if (val == 0) this.handleStop(this.current)});
|
|
|
|
}
|
|
|
|
handleStop(current) {
|
|
|
|
this.queue.shift();
|
|
|
|
this.playNext();
|
|
|
|
}
|
|
|
|
add(element) {
|
|
|
|
this.queue.push(element);
|
|
|
|
if (this.queue.length == 1) this.playNext();
|
|
|
|
}
|
2021-04-09 14:21:31 +00:00
|
|
|
flush() {
|
|
|
|
this.current.setVolume(0);
|
|
|
|
this.queue=[];
|
|
|
|
this.playNext();
|
|
|
|
}
|
2021-04-09 11:23:13 +00:00
|
|
|
}
|