Error: Cannot play a resource that has already ended.

Hey, i have thsi error when im trying to play a radio flux url like (for example) http://belrtl.ice.infomaniak.ch/belrtl-mp3-192.mp3 This is my code:
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, entersState } = require("@discordjs/voice");

const voice = require('@discordjs/voice');

module.exports = async (channel, track) => {


try {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guildId,
adapterCreator: channel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();

const resource = createAudioResource(track, { inlineVolume: true });
resource.volume.setVolume(0.1 * vol);

connection.on(voice.VoiceConnectionStatus.Ready, () => {
player.play(resource);
});

connection.on(voice.VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
await Promise.race([
entersState(connection, voice.VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, voice.VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
console.error(error)
} finally {
player.stop();
connection.destroy()
}
});


player.on('error', error => {
console.error(error);
player.stop();
connection.destroy();
});

connection.subscribe(player);
} catch (error) {
console.error(error);
}
};
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, entersState } = require("@discordjs/voice");

const voice = require('@discordjs/voice');

module.exports = async (channel, track) => {


try {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guildId,
adapterCreator: channel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();

const resource = createAudioResource(track, { inlineVolume: true });
resource.volume.setVolume(0.1 * vol);

connection.on(voice.VoiceConnectionStatus.Ready, () => {
player.play(resource);
});

connection.on(voice.VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
try {
await Promise.race([
entersState(connection, voice.VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, voice.VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
console.error(error)
} finally {
player.stop();
connection.destroy()
}
});


player.on('error', error => {
console.error(error);
player.stop();
connection.destroy();
});

connection.subscribe(player);
} catch (error) {
console.error(error);
}
};
31 Replies
d.js toolkit
d.js toolkit5mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
Millenium is here
Plz @ me discord js v14 with node 16.20 "discord.js": "^14.9.0", "@discordjs/opus": "^0.9.0", "@discordjs/voice": "^0.16.1",
ThePedroo
ThePedroo5mo ago
Where's the full error + backtrace?
Millenium is back
hi, can someone fix it ? throw new Error("Cannot play a resource that has already ended."); ^ Error: Cannot play a resource that has already ended.
ThePedroo
ThePedroo4w ago
Uh, the error explains itself
Millenium is back
but radio link is working, and bot was working with this link
ThePedroo
ThePedroo4w ago
You shall not re-use a stream
Millenium is back
so how to destroy it ?
ThePedroo
ThePedroo4w ago
You mean how to re-create it?
Millenium is back
ye, can i show you my code in dms ? it will be easier for you & me
ThePedroo
ThePedroo4w ago
Well, I don't need the code, so..
Millenium is back
ye but i think i do what u are saying
ThePedroo
ThePedroo4w ago
You can simply call the .createAudioResource function again and use it I need the full backtrace But the overlap of variable names can be problematic
Millenium is back
wdym ? the eror ? (srry im french) with this code its logging console.log('Audio player is idle. Restarting playback.'); to infinity
ThePedroo
ThePedroo4w ago
Let me find my old code to give as example 1s
Millenium is back
👍
ThePedroo
ThePedroo4w ago
This line is what you want to use it to check if the stream really ended The rest of the code may have some other codes you may find useful to improve yours But in any way, I'd name the resource variable names another name inside the .on As they have the same name as the one above them The At xxx messages
Millenium is back
/home/container/node_modules/@discordjs/voice/dist/index.js:1111
throw new Error("Cannot play a resource that has already ended.");
^
Error: Cannot play a resource that has already ended.
at AudioPlayer.play (/home/container/node_modules/@discordjs/voice/dist/index.js:1111:13)
at VoiceConnection.<anonymous> (/home/container/structures/functions/play.js:36:14)
at VoiceConnection.emit (node:events:513:28)
at VoiceConnection.set state [as state] (/home/container/node_modules/@discordjs/voice/dist/index.js:1755:12)
at VoiceConnection.onNetworkingStateChange (/home/container/node_modules/@discordjs/voice/dist/index.js:1897:18)
at Networking.emit (node:events:513:28)
at Networking.set state [as state] (/home/container/node_modules/@discordjs/voice/dist/index.js:606:10)
at Networking.onWsPacket (/home/container/node_modules/@discordjs/voice/dist/index.js:740:18)
at VoiceWebSocket.emit (node:events:525:35)
at VoiceWebSocket.onMessage (/home/container/node_modules/@discordjs/voice/dist/index.js:462:10)
/home/container/node_modules/@discordjs/voice/dist/index.js:1111
throw new Error("Cannot play a resource that has already ended.");
^
Error: Cannot play a resource that has already ended.
at AudioPlayer.play (/home/container/node_modules/@discordjs/voice/dist/index.js:1111:13)
at VoiceConnection.<anonymous> (/home/container/structures/functions/play.js:36:14)
at VoiceConnection.emit (node:events:513:28)
at VoiceConnection.set state [as state] (/home/container/node_modules/@discordjs/voice/dist/index.js:1755:12)
at VoiceConnection.onNetworkingStateChange (/home/container/node_modules/@discordjs/voice/dist/index.js:1897:18)
at Networking.emit (node:events:513:28)
at Networking.set state [as state] (/home/container/node_modules/@discordjs/voice/dist/index.js:606:10)
at Networking.onWsPacket (/home/container/node_modules/@discordjs/voice/dist/index.js:740:18)
at VoiceWebSocket.emit (node:events:525:35)
at VoiceWebSocket.onMessage (/home/container/node_modules/@discordjs/voice/dist/index.js:462:10)
ThePedroo
ThePedroo4w ago
What's the line 36 in your play.js file?
Millenium is back
player.play(currentResource);
ThePedroo
ThePedroo4w ago
Which one of them? Also Is track an URL or stream?
duck
duck4w ago
on the assumption this is the one that plays when the connection becomes ready, it doesn't look like you have any cleanup for this event listener connections are reused if not destroyed when connecting to a channel in the same guild, so if this code executes a second time for the same guild, this ready listener will execute a second time attempting to play the same resource furthermore a connection can in general become ready more than once on its own depending on a number of factors (e.g. if it disconnects due to poor network and reconnects)
ThePedroo
ThePedroo4w ago
Also, avoid re-using the same variable -- instead of checking if the stream is null, just do the other if to check when the stream "ends"
Millenium is back
But my code works on my computer and was working on my vps, but after 3 month 💤
ThePedroo
ThePedroo4w ago
Sometimes you only find bugs later on Discord voice didn't change since then, neither djs/voice unless you changed the version So it's a bug that has been already there since there, but you've never found out Take advantage of this and improve the code to be more robust (.e.g* proper checks and more robust design)
Millenium is back
I wasn't going to relaunch this bot because the code is too old... I'll see if I can do it again... if not, too bad!
ThePedroo
ThePedroo4w ago
Well, we're here to help. I recommend to first fix the main issue (duck said a possible issue, which even if it's not the issue here, it should be patched) then later on focus on make it more robust
Millenium is back
:Check: , thanks guys