bork
bork

Reputation: 21

How to play entire youtube playlist on discord.py bot with youtube-dl

I'm trying to make a discord music bot. I'm using youtube-dl to retrieve the info and ffmpeg to play the audio out. My bot can successfully queue and play regular videos but when it comes to playing the entire playlist, it doesn't work.

This is my code for playing playlists:

            if 'entries' in info:
                for i in info['entries']:
                    URL = i['formats'][0]['url']     
                    player = FFmpegPCMAudio(URL, **FFMPEG_OPTIONS)
                    queue.append(player)
                    source = queue.pop(0)
                    voice.play(player, after = lambda e: play_next(ctx, source))            
                    await ctx.send('playing song')

This downloads all the videos in the playlist but only plays the first one then shows this error: discord.errors.ClientException: Already playing audio.

Upvotes: 0

Views: 7886

Answers (2)

PandaStudios
PandaStudios

Reputation: 11

you can make a check if voice is playing audio or not and do stuff based on that value,

if voice is None:
    voice.play(player, after = lambda e: play_next(ctx, source))
else:
    print("Audio is playing!")

but by doing "voice is None" that means it's not only gonna check if audio is playing it's gonna check for everything with voice to do but the good thing by doing so is that you can check if the bot is still in the voice channel or not before the next song should start and join the channel again if the bot had some delay between playing the songs and left cause of inactivity,

voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice is_playing(): <--- something like this but that works!
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice is None:
        voiceChannel = discord.utils.get(ctx.guild.voice_channels, name=ctx.author.voice.channel.name)
        await voiceChannel.connect()
        voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
        voice.play(player, after = lambda e: play_next(ctx, source))
    else:
        voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
        voice.play(player, after = lambda e: play_next(ctx, source))

this last example might be a bit unfinnished but it should be very close to working if you can get the part where my arrow is pointing to work, but it's a good solution as far as i know hope this came to help i know i'm very late on this one :).

Upvotes: 1

Prune
Prune

Reputation: 77827

The problem is exactly what the error message tells you: you tried to play a file while one was already playing. You have to wait until the audio is done playing; your only delay is to stall until the download is finished.

You need to either look up how to make a blocking call to play (out of scope for Stack Overflow) or check the play time from the file's metadata, and sleep that long.

Does that get you moving?

Upvotes: 0

Related Questions