ted
ted
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by ted on 10/13/2024 in #djs-voice
After disconnecting once, audio starts audibly buffering
Here is part of my /play command:
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channelId,
guildId: interaction.guildId,
adapterCreator: interaction.member.voice.channel.guild.voiceAdapterCreator,
});

const resource = createAudioResource("an mp3 live link", {
inlineVolume: true,
});

const player = createAudioPlayer();

connection.subscribe(player);
player.play(resource);

player.on("stateChange", async (oldState: any, newState: any) => {
log(`AudioPlayer: ${oldState.status} -> ${newState.status}`, "debug");

switch (newState.status) {
case AudioPlayerStatus.Idle:
log("The player has become idle.", "debug");
const resource = createAudioResource("an mp3 live link", {
inlineVolume: true,
});
player.play(resource);
break;
case AudioPlayerStatus.AutoPaused:
try {
await Promise.race([
entersState(player, AudioPlayerStatus.Idle, 2_000),
entersState(player, AudioPlayerStatus.Buffering, 2_000),
entersState(player, AudioPlayerStatus.Playing, 2_000),
]);
} catch {
connection.disconnect();
}
break;
}
});

connection.on("stateChange", async (oldState: any, newState: any) => {
log(`VoiceConnection: ${oldState.status} -> ${newState.status}`, "debug");

switch (newState.status) {
case VoiceConnectionStatus.Disconnected:
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 2_000),
entersState(connection, VoiceConnectionStatus.Connecting, 2_000),
]);
} catch (error) {
connection.destroy();
}
break;
case VoiceConnectionStatus.Destroyed:
player.stop();
break;
}
});
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channelId,
guildId: interaction.guildId,
adapterCreator: interaction.member.voice.channel.guild.voiceAdapterCreator,
});

const resource = createAudioResource("an mp3 live link", {
inlineVolume: true,
});

const player = createAudioPlayer();

connection.subscribe(player);
player.play(resource);

player.on("stateChange", async (oldState: any, newState: any) => {
log(`AudioPlayer: ${oldState.status} -> ${newState.status}`, "debug");

switch (newState.status) {
case AudioPlayerStatus.Idle:
log("The player has become idle.", "debug");
const resource = createAudioResource("an mp3 live link", {
inlineVolume: true,
});
player.play(resource);
break;
case AudioPlayerStatus.AutoPaused:
try {
await Promise.race([
entersState(player, AudioPlayerStatus.Idle, 2_000),
entersState(player, AudioPlayerStatus.Buffering, 2_000),
entersState(player, AudioPlayerStatus.Playing, 2_000),
]);
} catch {
connection.disconnect();
}
break;
}
});

connection.on("stateChange", async (oldState: any, newState: any) => {
log(`VoiceConnection: ${oldState.status} -> ${newState.status}`, "debug");

switch (newState.status) {
case VoiceConnectionStatus.Disconnected:
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 2_000),
entersState(connection, VoiceConnectionStatus.Connecting, 2_000),
]);
} catch (error) {
connection.destroy();
}
break;
case VoiceConnectionStatus.Destroyed:
player.stop();
break;
}
});
Now, this works flawlessly. But, when I initiate a connection.disconnect() - in a seperate command - it does indeed leave the voice channel. But when I run /play again, while it does play, it is very buffery. I believe what is happening is the old player hasn't been destroyed, and my computer is trying to play 2 streams? Maybe not? - GuildVoiceStates intent is enabled - Here is my dependencies:
Core Dependencies
- @discordjs/voice: 0.17.0
- prism-media: 1.3.5
Opus Libraries
- @discordjs/opus: 0.9.0
- opusscript: 0.1.1
Encryption Libraries
- sodium-native: not found
- sodium: 3.0.2
- libsodium-wrappers: 0.7.15
- tweetnacl: 1.0.3
FFmpeg
- version: 6.0-essentials_build-www.gyan.dev
- libopus: yes
Core Dependencies
- @discordjs/voice: 0.17.0
- prism-media: 1.3.5
Opus Libraries
- @discordjs/opus: 0.9.0
- opusscript: 0.1.1
Encryption Libraries
- sodium-native: not found
- sodium: 3.0.2
- libsodium-wrappers: 0.7.15
- tweetnacl: 1.0.3
FFmpeg
- version: 6.0-essentials_build-www.gyan.dev
- libopus: yes
14 replies