首页 > 解决方案 > 在 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()函数生成下一行播放音频

线程会有用吗

标签: python-3.xmultithreadingdiscord.py

解决方案


如果你想让bot等待3秒,在文件播放方法之前使用 import timetime.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() 
 

推荐阅读