I am using discord.js v13 and attempting to play audio files using @discordjs/voice. My problem is that the bot will do everything correctly, but when the green circle that indicates that a sound is coming from the bot appears, I don't hear anything. I don't think this is an audio issue with my headphones because I can hear everything else fine, including other people on discord.
Here is my code:
const { getVoiceConnection, joinVoiceChannel, AudioPlayerStatus, createAudioResource, getNextResource, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice');
module.exports = {
name: "play",
category: "music",
description: "plays audio",
usage: "<id | mention>",
run: async (client, message, args) => {
const voiceChannel = message.member.voice.channel;
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
const audioPlayer = createAudioPlayer();
connection.subscribe(audioPlayer);
audioPlayer.play(createAudioResource('./sound/audio.mp3'));
}
}
Any help is appreciated.
Simply writing createAudioResource('./sound/audio.mp3') isn't the correct syntax to get the audio resource of the mp3 file you have, the only thing you miss is the createReadStream() which will create a readable stream before transforming it into an audio resource. In clear, just change
audioPlayer.play(createAudioResource('./sound/audio.mp3'));
to
audioPlayer.play(createAudioResource(createReadStream('./sound/audio.mp3')));
If this still doesn't solve your answer maybe try adding options like the inlineVolume option as the second parameter:
audioPlayer.play(createAudioResource(createReadStream('./sound/audio.mp3')),
{ inlineVolume: true });