From 73d174bc4ae2543f71c3f824204e0472b494b175 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Thu, 19 Mar 2026 16:18:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=98=9F=E5=88=97?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E6=97=B6=E9=9F=B3=E9=A2=91=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E9=94=99=E4=B9=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/handle/sendAudioHandle.py | 2 ++ .../core/utils/audioRateController.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 63a2411c..21cda9e6 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -280,6 +280,8 @@ async def send_tts_message(conn: "ConnectionHandler", state, text=None): await sendAudio(conn, audios) # 等待所有音频包发送完成 await _wait_for_audio_completion(conn) + # 停止音频发送循环 + conn.audio_rate_controller.stop_sending() # 清除服务端讲话状态 conn.clearSpeakStatus() diff --git a/main/xiaozhi-server/core/utils/audioRateController.py b/main/xiaozhi-server/core/utils/audioRateController.py index 01d71b05..29e203f2 100644 --- a/main/xiaozhi-server/core/utils/audioRateController.py +++ b/main/xiaozhi-server/core/utils/audioRateController.py @@ -43,6 +43,14 @@ class AudioRateController: def add_audio(self, opus_packet): """添加音频包到队列""" + # 如果队列之前为空,需要调整时间戳以保持播放时间连续 + # 这样工具调用等待期间,新加入的音频不会提前播放 + if len(self.queue) == 0 and self.play_position > 0: + self.start_timestamp = time.monotonic() - (self.play_position / 1000) + self.logger.bind(tag=TAG).debug( + f"队列从空恢复,重置时间戳,当前播放位置: {self.play_position}ms" + ) + self.queue.append(("audio", opus_packet)) # 相关事件处理 self.queue_empty_event.clear() @@ -55,6 +63,12 @@ class AudioRateController: Args: message_callback: 消息发送回调函数 async def() """ + if len(self.queue) == 0 and self.play_position > 0: + self.start_timestamp = time.monotonic() - (self.play_position / 1000) + self.logger.bind(tag=TAG).debug( + f"队列从空恢复,重置时间戳,当前播放位置: {self.play_position}ms" + ) + self.queue.append(("message", message_callback)) # 相关事件处理 self.queue_empty_event.clear()