正在生成不和谐语音通道上播放音频文件

时间:2020-07-20 16:57:30

标签: python-3.x multithreading discord.py

我正在制作一个不和谐的bot,它可以在语音聊天中读取带有文本到语音的reddit帖子,但是必须完全生成音频文件才能使bot能够开始播放,因此对于更长的帖子,等待时间超过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()函数生成下一行播放音频

线程有用吗

1 个答案:

答案 0 :(得分:0)

如果您希望机器人在使用文件播放方法之前等待3秒 import timetime.sleep(3)的数字3表示漫游器将等待多长时间的秒数,完整代码=

..
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() 
 
相关问题