Search code examples
javascriptdiscord.js

Can you stream Audio from a URL in a discord.js resource


How do I play audio from a url with discord.js v13.

I used this code and it didn't work.

    const connection = joinVoiceChannel({
        channelId: channel_id.id,
        guildId: guild_id,
        adapterCreator: message.guild.voiceAdapterCreator,
    });

    const player = createAudioPlayer();
    const resource = createAudioResource('http://radioplayer.kissfmuk.com/live/')
    player.play(resource)

    connection.subscribe(player)

I have activated all Intents, the bot shows a green circle but no audio is playing. Do you have an Idea how it works?


Solution

  • The answer was pretty simple, I just need to subscribe to the connection before I play the resource!

        const connection = joinVoiceChannel({
            channelId: channel_id.id,
            guildId: guild_id,
            adapterCreator: message.guild.voiceAdapterCreator,
        });
    
    
        const resource = 
        createAudioResource('https://streams.ilovemusic.de/iloveradio8.mp3', {
            inlineVolume: true
        })
    
        const player = createAudioPlayer();
        connection.subscribe(player)
        player.play(resource)