diff --git a/lib/modules/home/controllers/home_controller.dart b/lib/modules/home/controllers/home_controller.dart index a7729e491..7c1dddbc5 100644 --- a/lib/modules/home/controllers/home_controller.dart +++ b/lib/modules/home/controllers/home_controller.dart @@ -240,6 +240,7 @@ class HomeController extends GetxController { } void openTranslation() { + AgentService.stopAiSteam(); Get.toNamed(Routes.translationMiddleware); } 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 6d4d9583d..bc5c8bcab 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 @@ -1701,7 +1701,7 @@ class ChatApiStreamCallback: StreamCallback { return } - if (responseBuilder.isEmpty) { //关闭声音 + if (token != "" && responseBuilder.isEmpty) { //关闭声音 try agentService.audioPlayer?.stopAwaitSound() } @@ -1975,6 +1975,7 @@ class AudioPlayer { private var callingPlayer: AVAudioPlayer? private var awaitPlayer: AVAudioPlayer? // 新增:等待音效播放器 private var awaitTimer: DispatchSourceTimer? // 新增:等待音效超时定时器 + private var isAwaitSoundActive: Bool = false // 等待音效播放状态开关 private let logger = OSLog(subsystem: "com.yunqiinnovation.agent_service", category: "AudioPlayer") private let audioQueue = DispatchQueue(label: "agent.audio.player", qos: .userInitiated) @@ -2012,8 +2013,8 @@ class AudioPlayer { } func playAwaitSound() { - //先停止之前的等待音效和定时器 - stopAwaitSound() + // 若已处于等待音效播放状态,则直接返回,避免频繁启停 + if isAwaitSoundActive { return } // 修复:通过 agentService 引用访问 currentRecognitionMode if agentService?.currentRecognitionMode == "phone_call" {//电话模式不要嘟嘟嘟。影响回音消除功能 @@ -2022,6 +2023,7 @@ class AudioPlayer { // 播放等待音效 playLoopSound(named: "await", fileType: "mp3", player: &awaitPlayer,volume:0.2) + isAwaitSoundActive = true // 启动超时定时器 startAwaitTimeout() @@ -2041,10 +2043,20 @@ class AudioPlayer { } func stopAwaitSound() { + if !self.isAwaitSoundActive { return } + self.isAwaitSoundActive = false // 确保在主线程执行音频停止操作 DispatchQueue.main.async { [weak self] in guard let self = self else { return } + // 若未处于等待音效播放状态,直接返回,避免高频空调用 + let wasPlaying = self.awaitPlayer?.isPlaying ?? false + let hadTimer = self.awaitTimer != nil + // 幂等短路:若既未播放也无定时器,直接返回,避免高频空调用带来的开销 + if !wasPlaying && !hadTimer { + return + } + // 停止音频播放 if let player = self.awaitPlayer { if player.isPlaying { @@ -2054,8 +2066,9 @@ class AudioPlayer { } // 停止超时定时器 - self.stopAwaitTimeout() - + if hadTimer { + self.stopAwaitTimeout() + } os_log("停止等待音效播放", log: self.logger, type: .debug) } }