【问题标题】:Pausing, queuing, and looping songs with discord.py使用 discord.py 暂停、排队和循环歌曲
【发布时间】:2021-03-21 16:12:51
【问题描述】:

我有这段代码,它可以在语音频道中播放歌曲并且工作正常,但是如何生成允许排队、暂停和循环的代码?

@client.command(pass_context=True)
async def play(ctx, url):

    if not ctx.message.author.voice:
        await ctx.send('Please enter the voice channel')
        return

    else:
        channel = ctx.message.author.voice.channel

    voice_client = await channel.connect()

    guild = ctx.message.guild

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        file = ydl.extract_info(url, download=True)
        path = str(file['title']) + "-" + str(file['id'] + ".mp3")

    voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
    voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)

    await ctx.send(f'**NOW MUSIC: **{url}')

    while voice_client.is_playing():
        await asyncio.sleep(1)
    else:
        await voice_client.disconnect()
        print("Disconnected")

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    暂停命令示例:

    @client.command
    async def pause(ctx):
        server = ctx.message.guild
        voice_channel = server.voice_client                
        voice_channel.pause()
        print('pause')
    

    关于队列,您可以创建队列歌曲列表。检查正在播放的歌曲是否会将歌曲附加到队列中,播放当前歌曲后,您需要播放队列[0]歌曲。对不起我的英语

    【讨论】:

      【解决方案2】:

      https://discordpy.readthedocs.io/en/latest/api.html#voice

      您可以看到存储在您的voice_client 中的Voice 对象有一个pause()resume() 方法:暂停很简单。

      对于排队和循环,繁重的工作必须由您完成。歌曲结束后,您必须再次致电play(),然后让它再次播放同一首歌曲(循环)或播放您将存储在数组或其他位置中的下一首歌曲(排队)。

      好消息是,discord.py 允许您避免使用烦人的技术来知道歌曲何时结束:反复调用 is_playing() 直到它返回 False。正如您在代码中所写的那样,play() 还将一个名为 after 的可调用对象作为参数,该可调用对象在歌曲结束时(或出现错误时!)调用。

      在您的情况下,这意味着您在一首歌曲之后调用的 endSong() 函数必须知道您是否应该将同一首歌曲或另一首歌曲排队,然后调用 play() 来启动它。如果您无法理解如何执行此操作,请展示您的尝试,以便我们为您提供更具体的建议。

      【讨论】:

        猜你喜欢
        • 2020-08-17
        • 2021-09-03
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多