【问题标题】:Discord.py Bot How to play audio from local filesDiscord.py Bot 如何从本地文件播放音频
【发布时间】:2020-11-12 03:30:48
【问题描述】:

基本上是标题。我已经安装了 ffmpeg 和 discord.py[audio]。我只需要了解它是如何工作的。找不到本地音频文件的任何教程。而且我无法从文档中理解任何内容:/

【问题讨论】:

标签: python audio bots discord.py


【解决方案1】:

这是一个播放本地音频文件的功能。我在使用 FFmpeg 时遇到了问题,所以我硬编码了 .exe 路径。另外,我的本地文件的文件路径有问题,所以我把绝对路径放进去。这个函数也会在音频播放完毕后删除调用它的命令,保持整洁。

 @bot.command(name="<command_name>")
    async def <function_name>(ctx):
        # Gets voice channel of message author
        voice_channel = ctx.author.channel
        channel = None
        if voice_channel != None:
            channel = voice_channel.name
            vc = await voice_channel.connect()
            vc.play(discord.FFmpegPCMAudio(executable="C:/ffmpeg/bin/ffmpeg.exe", source="C:<path_to_file>"))
            # Sleep while audio is playing.
            while vc.is_playing():
                sleep(.1)
            await vc.disconnect()
        else:
            await ctx.send(str(ctx.author.name) + "is not in a channel.")
        # Delete command after the audio is done playing.
        await ctx.message.delete()

【讨论】:

  • 效果很好。谢谢。我还想在这里快速指出一些事情。如果你们遇到像'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 这样的错误... 将r 放在这样的路径字符串之前:...source = r"C:\Users..." 或使用/ 而不是“\”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 2021-06-05
  • 2021-08-23
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多