From 561e1760cb4addb429c853e12a252dfb32daa2a9 Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Sun, 3 Aug 2025 15:24:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(tts):=20=E5=9C=A8TTS=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E6=97=B6=E5=81=9C=E6=AD=A2=E6=B0=94=E6=B3=A1=E9=9F=B3=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=A3=B0=E9=9F=B3=E9=87=8D=E5=8F=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改iOS和Android端的TTS播放逻辑,在开始流式TTS、播放完成、取消或出错时立即停止气泡音播放。避免两种声音同时播放造成干扰,提升用户体验。 --- .../yunqiinnovation/agent_service/AgentService.kt | 13 ++++++++++++- .../Sources/agent_service/AgentServiceImpl.swift | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index 1fdaea5df..a1921b405 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -316,6 +316,8 @@ object AgentService : CoroutineScope { _isTtsSpeaking.set(true) restartIdleCheck() sendEvent("tts_started", mapOf("status" to "started")) + // 停止气泡音,避免与TTS同时播放 + audioPlayer?.stopAudio() } TtsEventType.SYNTHESIS_COMPLETED -> { @@ -328,11 +330,15 @@ object AgentService : CoroutineScope { _isTtsSpeaking.set(false) restartIdleCheck() sendEvent("tts_canceled", mapOf("status" to "canceled")) + // TTS取消时停止气泡音 + audioPlayer?.stopAudio() } TtsEventType.PLAYBACK_STARTED -> { //开始AI播报内容 restartIdleCheck() sendEvent("playback_started", mapOf("status" to "playback_started")) + // 确保气泡音已停止,避免与TTS播放重叠 + audioPlayer?.stopAudio() // 发送AI说话开始广播 val intent = Intent("com.yunqiinnovation.music_service.AI_SAY_START").apply { @@ -355,6 +361,8 @@ object AgentService : CoroutineScope { TtsEventType.ERROR -> { _isTtsSpeaking.set(false) restartIdleCheck() + // TTS错误时停止气泡音 + audioPlayer?.stopAudio() val params = event.params val code = params["errorCode"] as? String ?: "UNKNOWN_ERROR" val message = params["errorMessage"] as? String ?: "未知错误" @@ -663,6 +671,8 @@ object AgentService : CoroutineScope { // 并行执行停止操作,加快响应速度 stopTts() stopAiStream() + // 停止气泡音,避免在打断后继续播放 + audioPlayer?.stopAudio() // 记录打断耗时 @@ -888,7 +898,8 @@ object AgentService : CoroutineScope { responseBuilder.append(token) if (speakResponse && reply && broadcast) { ttsService?.speakStream(token) - // audioPlayer?.stopAudio() + // 在开始流式TTS时立即停止气泡音 + audioPlayer?.stopAudio() } if (reply && token.isNotEmpty()) { // 发送流式回复token diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index bfac028cd..684616bc8 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -996,7 +996,8 @@ class ChatApiStreamCallback: StreamCallback { if speakResponse && reply && broadcast{ try agentService.azureTtsHelper?.speakStream(token) -// try agentService.audioPlayer?.stopAwaitSound() + // 在开始流式TTS时立即停止气泡音 + try agentService.audioPlayer?.stopAwaitSound() } if (reply && token != "") { try agentService.sendEvent(name: "assistant_token", data: ["token": token])