python-3.x - 在 Discord 语音通道上播放音频文件 wile 文件正在生成
问题描述
我正在制作一个不和谐的机器人,它可以在语音聊天中读取带有文本到语音的 reddit 帖子,但必须完全生成音频文件才能让机器人开始播放,因此对于较长的帖子,等待时间超过 10 分钟。但是文件的生成速度比你听它的速度更快,所以理论上我可以在文本下载完成后制作它,它可以等待 3 秒然后播放文件,音频就像文件已经生成一样工作
async def on_message(self, ctx):
if ctx.content.startswith("-p"):
connected = ctx.author.voice
if connected: #if the poster in conected to a voice channel
if is_connected(self) == False: #if the bot not connected to a voice chat it will join the posters voice chat
global voice
voice = await connected.channel.connect()
say(ctx.content.split()[1]) #uses URL of a reddit to make a mp3 file of a TTS reading the post
player = voice.play(discord.FFmpegPCMAudio("out.mp3")) #stream the auidio file to the voice channel
player.start()
该say()
函数生成下一行播放音频
线程会有用吗
解决方案
如果你想让bot等待3秒,在文件播放方法之前使用
import time
比time.sleep(3)
数字3表示bot将等待多长时间的秒数,完整代码=
..
import time
..
async def on_message(self, ctx):
if ctx.content.startswith("-p"):
connected = ctx.author.voice
if connected: #if the poster in conected to a voice channel
if is_connected(self) == False: #if the bot not connected to a voice chat it will join the posters voice chat
global voice
voice = await connected.channel.connect()
say(ctx.content.split()[1]) #uses URL of a reddit to make a mp3 file of a TTS reading the post
time.sleep(3)
player = voice.play(discord.FFmpegPCMAudio("out.mp3")) #stream the auidio file to the voice channel
player.start()
推荐阅读
- python - Pandas:根据日期对值进行分组
- excel - 如何通过 Next Row 的每个单元格继续循环?
- menu - 如何制作一个不占用自己空间的元素并通过覆盖它们来显示在其他元素之上
- javascript - 如何摆脱具有早期返回的多个嵌套 switchMap
- reactjs - 尽管我使用的是 prevState,但 SetTimeout 会改变我的状态
- python - python中的setup.py:如何安装子包?
- android - Android ContentResolver.openFileDescriptor() 抛出 java.lang.IllegalStateException: java.io.IOException: 无法编辑
- c - 添加动态分配打印地址而不是原始值的矩阵,为什么?
- python - 保存时xarray netcdf更改数据类型
- laravel - Laravel 中的多个 where 和 or 条件