首页 > 解决方案 > Discord.py 循环播放音频源

问题描述

有谁知道如何在 FFmpeg 中循环相同的源代码?

这是我的代码:

@bot.command(pass_context = True)
async def join(ctx):

if (ctx.author.voice):
    channel = ctx.message.author.voice.channel
    voice = await channel.connect()
    source = FFmpegPCMAudio('test.mp3')
    player = voice.play(source)
else:
    await ctx.send('You need to be in a Voice-Channel.')

我想要永久循环播放音频文件“test.mp3”。我在互联网上搜索过,但那里给出的所有结果都已过时。

标签: pythonffmpegdiscorddiscord.pyffmpeg-python

解决方案


这是一种知道歌曲何时播放完毕的方法。

if (ctx.author.voice):
    finished = False 
    channel = ctx.message.author.voice.channel
    voice = await channel.connect()

    voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: finished = True)

使用 lambda 函数,您可以实现循环结构以在完成后重播歌曲。


推荐阅读