問題描述
基本上是標(biāo)題.我已經(jīng)安裝了 ffmpeg 和 discord.py[audio].我只需要了解它是如何工作的.找不到本地音頻文件的任何教程.而且我無法從文檔中理解任何內(nèi)容:/
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 :/
推薦答案
這是一個(gè)播放本地音頻文件的功能.我在使用 FFmpeg 時(shí)遇到了問題,所以我硬編碼了 .exe 路徑.另外,我本地文件的文件路徑有問題,所以我把絕對(duì)路徑放進(jìn)去.這個(gè)函數(shù)也會(huì)在音頻播放完畢后刪除調(diào)用它的命令,保持整潔.
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()
這篇關(guān)于Discord.py Bot 如何從本地文件播放音頻的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!