Browse Source

fix(tts): 在TTS播放时停止气泡音避免声音重叠

修改iOS和Android端的TTS播放逻辑,在开始流式TTS、播放完成、取消或出错时立即停止气泡音播放。避免两种声音同时播放造成干扰,提升用户体验。
newdev_shunjiawei
lxm 1 year ago
parent
commit
561e1760cb
  1. 13
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  2. 3
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift

13
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -316,6 +316,8 @@ object AgentService : CoroutineScope {
_isTtsSpeaking.set(true) _isTtsSpeaking.set(true)
restartIdleCheck() restartIdleCheck()
sendEvent("tts_started", mapOf("status" to "started")) sendEvent("tts_started", mapOf("status" to "started"))
// 停止气泡音,避免与TTS同时播放
audioPlayer?.stopAudio()
} }
TtsEventType.SYNTHESIS_COMPLETED -> { TtsEventType.SYNTHESIS_COMPLETED -> {
@ -328,11 +330,15 @@ object AgentService : CoroutineScope {
_isTtsSpeaking.set(false) _isTtsSpeaking.set(false)
restartIdleCheck() restartIdleCheck()
sendEvent("tts_canceled", mapOf("status" to "canceled")) sendEvent("tts_canceled", mapOf("status" to "canceled"))
// TTS取消时停止气泡音
audioPlayer?.stopAudio()
} }
TtsEventType.PLAYBACK_STARTED -> { //开始AI播报内容 TtsEventType.PLAYBACK_STARTED -> { //开始AI播报内容
restartIdleCheck() restartIdleCheck()
sendEvent("playback_started", mapOf("status" to "playback_started")) sendEvent("playback_started", mapOf("status" to "playback_started"))
// 确保气泡音已停止,避免与TTS播放重叠
audioPlayer?.stopAudio()
// 发送AI说话开始广播 // 发送AI说话开始广播
val intent = val intent =
Intent("com.yunqiinnovation.music_service.AI_SAY_START").apply { Intent("com.yunqiinnovation.music_service.AI_SAY_START").apply {
@ -355,6 +361,8 @@ object AgentService : CoroutineScope {
TtsEventType.ERROR -> { TtsEventType.ERROR -> {
_isTtsSpeaking.set(false) _isTtsSpeaking.set(false)
restartIdleCheck() restartIdleCheck()
// TTS错误时停止气泡音
audioPlayer?.stopAudio()
val params = event.params val params = event.params
val code = params["errorCode"] as? String ?: "UNKNOWN_ERROR" val code = params["errorCode"] as? String ?: "UNKNOWN_ERROR"
val message = params["errorMessage"] as? String ?: "未知错误" val message = params["errorMessage"] as? String ?: "未知错误"
@ -663,6 +671,8 @@ object AgentService : CoroutineScope {
// 并行执行停止操作,加快响应速度 // 并行执行停止操作,加快响应速度
stopTts() stopTts()
stopAiStream() stopAiStream()
// 停止气泡音,避免在打断后继续播放
audioPlayer?.stopAudio()
// 记录打断耗时 // 记录打断耗时
@ -888,7 +898,8 @@ object AgentService : CoroutineScope {
responseBuilder.append(token) responseBuilder.append(token)
if (speakResponse && reply && broadcast) { if (speakResponse && reply && broadcast) {
ttsService?.speakStream(token) ttsService?.speakStream(token)
// audioPlayer?.stopAudio() // 在开始流式TTS时立即停止气泡音
audioPlayer?.stopAudio()
} }
if (reply && token.isNotEmpty()) { if (reply && token.isNotEmpty()) {
// 发送流式回复token // 发送流式回复token

3
local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift

@ -996,7 +996,8 @@ class ChatApiStreamCallback: StreamCallback {
if speakResponse && reply && broadcast{ if speakResponse && reply && broadcast{
try agentService.azureTtsHelper?.speakStream(token) try agentService.azureTtsHelper?.speakStream(token)
// try agentService.audioPlayer?.stopAwaitSound() // 在开始流式TTS时立即停止气泡音
try agentService.audioPlayer?.stopAwaitSound()
} }
if (reply && token != "") { if (reply && token != "") {
try agentService.sendEvent(name: "assistant_token", data: ["token": token]) try agentService.sendEvent(name: "assistant_token", data: ["token": token])

Loading…
Cancel
Save