You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pycord开发Discord录音机器人时调用stop_recording触发Decoder Process Killed循环报错

Pycord语音机器人调用stop_recording时持续抛出'Decoder Process Killed'错误

我用Pycord开发了一款Discord机器人,用于录制语音频道中的用户音频。目前遇到的问题是,调用voice_client的stop_recording方法时,程序会持续抛出'Decoder Process Killed'错误。

相关代码实现

class VoiceSession():
    """
    A voice session with the bot
    """

    def __init__(self, bot, ctx):
        self.bot = bot
        self.ctx = ctx
        self.channel = ctx.channel
        self.member = ctx.author
        self.history = []
        self.recording = ctx.voice_client.recording
        self.chat_session = ChatGPTSession()
        self.voice_client = ctx.voice_client
        self.audio_queue = asyncio.Queue()
        self.id = random.randint(0, 1000000)
        self.recording_task = None
        self.sink = discord.sinks.MP3Sink()
        self.connections = {}

    def start_recording_task(self):
        self.recording_task = asyncio.create_task(self.recording_loop())

    def stop_recording_task(self):
        if self.recording_task is not None:
            self.recording_task.cancel()
            self.recording_task = None

    async def recording_loop(self):
        canspeakagain = True
        while True:
            if not self.recording and canspeakagain:
                await self.start_record(self.ctx)
                canspeakagain = False
            if self.recording and self.voice_client.finished_speaking:
                await asyncio.sleep(1)
                await self.stop_record(self.ctx)


    async def start(self):
        self.start_recording_task()

    async def stop(self):
        self.stop_recording_task()

    async def start_record(self, ctx):
        voice = ctx.author.voice

        vc = self.voice_client

        self.connections.update({ctx.guild.id: vc})  # Updating the cache with the guild and channel.
        
        vc.start_recording(
            discord.sinks.WaveSink(),  # The sink type to use.
            self.finished_callback,  # What to do once done.
            ctx.channel  # The channel to disconnect from.
        )
        print("started recording")
        self.recording = True

    async def finished_callback(self, sink: discord.sinks, channel: discord.TextChannel, *args):  # Our voice client already passes these in.
        print('callback called')
        recorded_users = [  # A list of recorded users
            f"<@{user_id}>"
            for user_id, audio in sink.audio_data.items()
        ]
        files = [discord.File(audio.file, f"{user_id}.{sink.encoding}") for user_id, audio in sink.audio_data.items()]  # List down the files.
        await channel.send(f"finished recording audio for: {', '.join(recorded_users)}.", files=files)  # Send a message with the accumulated files.

    async def stop_record(self, ctx):
        if ctx.guild.id in self.connections:
            vc = self.connections[ctx.guild.id]

            vc.stop_recording()
            del self.connections[ctx.guild.id]
            await ctx.delete()
        else:
            await ctx.respond("I am currently not recording here.")

控制台输出

Starting...
Bot is ready.
started recording
finished speaking
finished speaking
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
...

已尝试的解决方案

  • 将Thread替换为asyncio.Task,使录制循环异步运行
  • 使用async with替代try/except,确保错误发生时语音客户端能正确断开连接
  • 添加打印语句定位错误位置,确认错误出现在执行vc.stop_recording()之后

内容的提问来源于stack exchange,提问作者Bogdan Perju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:57:42