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()