mirror of
https://github.com/YuzuZensai/NekoMelody.git
synced 2026-01-31 14:57:58 +00:00
✨ feat: Separated example
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import NekoMelody, { Player } from "../src";
|
import NekoMelody, { Player } from "../src";
|
||||||
|
|
||||||
import Speaker from "speaker";
|
|
||||||
import ffmpeg from "fluent-ffmpeg";
|
|
||||||
import {
|
import {
|
||||||
NoSubscriberBehavior,
|
NoSubscriberBehavior,
|
||||||
VoiceConnectionStatus,
|
VoiceConnectionStatus,
|
||||||
@@ -76,6 +74,7 @@ const main = async () => {
|
|||||||
//inlineVolume: true,
|
//inlineVolume: true,
|
||||||
});
|
});
|
||||||
discordPlayer.play(resource);
|
discordPlayer.play(resource);
|
||||||
|
player.startCurrentStream();
|
||||||
|
|
||||||
discordPlayer.on("stateChange", (oldState, newState) => {
|
discordPlayer.on("stateChange", (oldState, newState) => {
|
||||||
console.log("State change", oldState.status, newState.status);
|
console.log("State change", oldState.status, newState.status);
|
||||||
@@ -89,50 +88,4 @@ const main = async () => {
|
|||||||
await player.enqueue(`https://www.youtube.com/watch?v=${videoId2}`);
|
await player.enqueue(`https://www.youtube.com/watch?v=${videoId2}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: player end event to automate changing the stream
|
|
||||||
let lastFFmpeg: ffmpeg.FfmpegCommand | null = null;
|
|
||||||
let lastSpeaker: Speaker | null = null;
|
|
||||||
const playSpeaker = async (player: Player) => {
|
|
||||||
if (!player.stream) {
|
|
||||||
console.error("No input stream");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// A function that resolves when the speaker is closed and the ffmpeg process is killed
|
|
||||||
const closeSpeaker = () => {
|
|
||||||
return new Promise<void>((resolve) => {
|
|
||||||
if (lastSpeaker) {
|
|
||||||
lastSpeaker.on("close", () => {
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
if (lastFFmpeg) lastFFmpeg.kill("SIGKILL");
|
|
||||||
lastSpeaker.close(true);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
await closeSpeaker();
|
|
||||||
|
|
||||||
// Create the Speaker instance
|
|
||||||
const speaker = new Speaker();
|
|
||||||
lastSpeaker = speaker;
|
|
||||||
|
|
||||||
// PCM data from stdin gets piped into the speaker
|
|
||||||
const ffmpegProcess = ffmpeg()
|
|
||||||
.input(player.stream)
|
|
||||||
.format("s16le") // Output format (PCM 16-bit little-endian)
|
|
||||||
.audioChannels(2)
|
|
||||||
.audioFrequency(44100)
|
|
||||||
.on("error", (err) => {
|
|
||||||
console.error("[FFmpeg] > Error:", err.message);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Pipe the ffmpeg output to the speaker
|
|
||||||
ffmpegProcess.pipe(speaker, { end: true });
|
|
||||||
|
|
||||||
lastFFmpeg = ffmpegProcess;
|
|
||||||
};
|
|
||||||
|
|
||||||
main();
|
main();
|
||||||
82
example/speaker.ts
Normal file
82
example/speaker.ts
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import NekoMelody, { Player } from "../src";
|
||||||
|
|
||||||
|
import Speaker from "speaker";
|
||||||
|
import ffmpeg from "fluent-ffmpeg";
|
||||||
|
import { YtDlpProvider } from "../src/providers";
|
||||||
|
import { AudioInformation } from "../src/providers/base";
|
||||||
|
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
const videoId = "2gigEGxnsmo";
|
||||||
|
const videoId2 = "oM-JneFEdBk";
|
||||||
|
|
||||||
|
// Providers
|
||||||
|
const providers = [new YtDlpProvider()];
|
||||||
|
const player = NekoMelody.createPlayer(providers);
|
||||||
|
|
||||||
|
player.on("play", async (information: AudioInformation) => {
|
||||||
|
await playSpeaker(player);
|
||||||
|
player.startCurrentStream();
|
||||||
|
});
|
||||||
|
|
||||||
|
await player.enqueue(`https://www.youtube.com/watch?v=${videoId}`);
|
||||||
|
await player.enqueue(`https://www.youtube.com/watch?v=${videoId2}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
let lastFFmpeg: ffmpeg.FfmpegCommand | null = null;
|
||||||
|
let lastSpeaker: Speaker | null = null;
|
||||||
|
|
||||||
|
const playSpeaker = async (player: Player) => {
|
||||||
|
if (!player.stream) {
|
||||||
|
console.error("No input stream");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A function that resolves when the speaker is closed and the ffmpeg process is killed
|
||||||
|
const closeSpeaker = () => {
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
if (lastSpeaker) {
|
||||||
|
lastSpeaker.on("close", () => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
if (lastFFmpeg) lastFFmpeg.kill("SIGKILL");
|
||||||
|
lastSpeaker.close(true);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
await closeSpeaker();
|
||||||
|
|
||||||
|
// Create the Speaker instance
|
||||||
|
const speaker = new Speaker();
|
||||||
|
lastSpeaker = speaker;
|
||||||
|
|
||||||
|
// PCM data from stdin gets piped into the speaker
|
||||||
|
const ffmpegProcess = ffmpeg()
|
||||||
|
.input(player.stream)
|
||||||
|
.format("s16le") // Output format (PCM 16-bit little-endian)
|
||||||
|
.audioChannels(2)
|
||||||
|
.audioFrequency(44100)
|
||||||
|
// .on("end", async () => {
|
||||||
|
// await closeSpeaker();
|
||||||
|
// })
|
||||||
|
.on("error", (err) => {
|
||||||
|
console.error("[FFmpeg] > Error:", err.message);
|
||||||
|
});
|
||||||
|
|
||||||
|
speaker.on("unpipe", () => {
|
||||||
|
console.log("Speaker ended");
|
||||||
|
player.endCurrentStream();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Pipe the ffmpeg output to the speaker
|
||||||
|
ffmpegProcess.pipe(speaker, { end: true });
|
||||||
|
|
||||||
|
lastFFmpeg = ffmpegProcess;
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
||||||
Reference in New Issue
Block a user