mirror of
https://github.com/YuzuZensai/NekoMelody.git
synced 2026-01-31 14:57:58 +00:00
✨ feat: Queue system
This commit is contained in:
@@ -1,22 +1,65 @@
|
||||
import { Readable, Stream } from "stream";
|
||||
import { Provider } from "../providers/base";
|
||||
import { EventEmitter, Readable, Stream } from "stream";
|
||||
import { AudioInformation, Provider } from "../providers/base";
|
||||
import { SeekableStream } from "../utils/SeekableStream";
|
||||
|
||||
export class Player {
|
||||
private providers: Provider[];
|
||||
private currentProvider: Provider | null = null;
|
||||
private queue: AudioInformation[] = [];
|
||||
private playerEvent: EventEmitter = new EventEmitter();
|
||||
private paused: boolean = false;
|
||||
private currentAudioInformation: AudioInformation | null = null;
|
||||
|
||||
public _stream: SeekableStream | null = null;
|
||||
|
||||
get stream() {
|
||||
return this._stream?.stream;
|
||||
}
|
||||
|
||||
constructor(providers: Provider[]) {
|
||||
this.providers = providers;
|
||||
}
|
||||
|
||||
public async play(url: string, seekTime: number = 0) {
|
||||
public get stream() {
|
||||
return this._stream?.stream;
|
||||
}
|
||||
|
||||
private _createStream(
|
||||
information: AudioInformation,
|
||||
url: string,
|
||||
seekTime: number,
|
||||
) {
|
||||
// If already playing, destroy the current stream
|
||||
if (this._stream) {
|
||||
this._stream.destroy();
|
||||
}
|
||||
|
||||
this._stream = new SeekableStream(information, url, seekTime);
|
||||
this.currentAudioInformation = information;
|
||||
this._stream.on("destroy", () => {
|
||||
console.log("Stream destroyed, total song", this.queue.length);
|
||||
if (this.queue.length > 0) {
|
||||
const next = this.queue.shift();
|
||||
console.log("Playing next in queue");
|
||||
if (next) {
|
||||
this._createStream(next, next.url, 0);
|
||||
}
|
||||
} else {
|
||||
this._stream = null;
|
||||
this.currentAudioInformation = null;
|
||||
}
|
||||
});
|
||||
|
||||
this.playerEvent.emit("play", information);
|
||||
}
|
||||
|
||||
public endCurrentStream() {
|
||||
if (this._stream) {
|
||||
this._stream.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public on(event: string, listener: (...args: any[]) => void) {
|
||||
this.playerEvent.on(event, listener);
|
||||
}
|
||||
|
||||
public async getInformation(url: string) {
|
||||
if (!this.currentProvider) {
|
||||
const providers = this.providers.filter((provider) =>
|
||||
provider.canPlay(url),
|
||||
@@ -29,19 +72,42 @@ export class Player {
|
||||
this.currentProvider = providers[0];
|
||||
}
|
||||
|
||||
const information = await this.currentProvider.getInformation(url);
|
||||
return await this.currentProvider.getInformation(url);
|
||||
}
|
||||
|
||||
public async play(url: string, seekTime: number = 0) {
|
||||
const information = await this.getInformation(url);
|
||||
//console.log(information);
|
||||
|
||||
if (information.livestream)
|
||||
// TODO: Implement livestreams
|
||||
throw new Error("Livestreams are not supported yet");
|
||||
|
||||
// If already playing, destroy the current stream
|
||||
if (this._stream) {
|
||||
this._stream.destroy();
|
||||
this._createStream(information, url, seekTime);
|
||||
}
|
||||
|
||||
public async enqueue(url: string, seekTime: number = 0) {
|
||||
const information = await this.getInformation(url);
|
||||
|
||||
if (information.livestream)
|
||||
// TODO: Implement livestreams
|
||||
throw new Error("Livestreams are not supported yet");
|
||||
|
||||
this.playerEvent.emit("enqueue", information);
|
||||
|
||||
// If queue is empty, no stream is playing and not paused, play the current URL
|
||||
if (
|
||||
this.queue.length === 0 &&
|
||||
!this.currentAudioInformation &&
|
||||
!this._stream &&
|
||||
!this.paused
|
||||
) {
|
||||
this._createStream(information, url, seekTime);
|
||||
} else {
|
||||
this.queue.push(information);
|
||||
}
|
||||
|
||||
this._stream = new SeekableStream(information, url, seekTime);
|
||||
console.log("Enqueued", url);
|
||||
}
|
||||
|
||||
public async seek(time: number) {
|
||||
|
||||
Reference in New Issue
Block a user