mirror of
https://github.com/YuzuZensai/NekoMelody.git
synced 2026-01-31 04:42:51 +00:00
✨ feat: Rework on streaming to use WebM format
This commit is contained in:
50
src/player/index.ts
Normal file
50
src/player/index.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Readable, Stream } from "stream";
|
||||
import { Provider } from "../providers/base";
|
||||
import { SeekableStream } from "../utils/SeekableStream";
|
||||
|
||||
export class Player {
|
||||
private providers: Provider[];
|
||||
private currentProvider: Provider | null = null;
|
||||
|
||||
public _stream: SeekableStream | null = null;
|
||||
|
||||
get stream() {
|
||||
return this._stream?.stream;
|
||||
}
|
||||
|
||||
constructor(providers: Provider[]) {
|
||||
this.providers = providers;
|
||||
}
|
||||
|
||||
public async play(url: string) {
|
||||
if (!this.currentProvider) {
|
||||
const providers = this.providers.filter((provider) =>
|
||||
provider.canPlay(url),
|
||||
);
|
||||
|
||||
if (providers.length === 0) {
|
||||
throw new Error("No provider can play this URL");
|
||||
}
|
||||
|
||||
this.currentProvider = providers[0];
|
||||
}
|
||||
|
||||
// TODO: Handle if already playing
|
||||
|
||||
const information = await this.currentProvider.getInformation(url);
|
||||
//console.log(information);
|
||||
|
||||
if (information.livestream)
|
||||
// TODO: Implement livestreams
|
||||
throw new Error("Livestreams are not supported yet");
|
||||
else this._stream = new SeekableStream(information, url);
|
||||
}
|
||||
|
||||
public getCurrentSampleRate() {
|
||||
return this._stream?.information.bitrate || 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function createPlayer(providers: Provider[]) {
|
||||
return new Player(providers);
|
||||
}
|
||||
Reference in New Issue
Block a user