Discord.py Bot How to play audio from local files(Discord.py Bot 如何从本地文件播放音频)
问题描述
基本上是标题.我已经安装了 ffmpeg 和 discord.py[audio].我只需要了解它是如何工作的.找不到本地音频文件的任何教程.而且我无法从文档中理解任何内容:/
Basically the title. I installed ffmpeg and discord.py[audio] already. I just need to learn how it works. couldn't find any tutorial for local audio files. and I cant understand anything from documentations :/
推荐答案
这是一个播放本地音频文件的功能.我在使用 FFmpeg 时遇到了问题,所以我硬编码了 .exe 路径.另外,我本地文件的文件路径有问题,所以我把绝对路径放进去.这个函数也会在音频播放完毕后删除调用它的命令,保持整洁.
That's a function that plays a local audio file. I had problems with FFmpeg, so I hardcoded the .exe path. Also, I had a problem with the file path to the local file, so I put the absolute path in. This function also deletes the command that called it after the audio is done playing, keeping things tidy.
@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()
这篇关于Discord.py Bot 如何从本地文件播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.py Bot 如何从本地文件播放音频
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01