From 870e60963b1d6b0952f15e83542f925443dd5460 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 4 Aug 2025 11:11:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=9F=B3=E6=95=88=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E8=B6=85=E6=97=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_service/AgentServiceImpl.swift | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) 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 684616bc8..b884a0c71 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 @@ -997,7 +997,7 @@ class ChatApiStreamCallback: StreamCallback { if speakResponse && reply && broadcast{ try agentService.azureTtsHelper?.speakStream(token) // 在开始流式TTS时立即停止气泡音 - try agentService.audioPlayer?.stopAwaitSound() + // try agentService.audioPlayer?.stopAwaitSound() } if (reply && token != "") { try agentService.sendEvent(name: "assistant_token", data: ["token": token]) @@ -1220,8 +1220,12 @@ class AudioPlayer { private var player: AVAudioPlayer? private var callingPlayer: AVAudioPlayer? private var awaitPlayer: AVAudioPlayer? // 新增:等待音效播放器 + private var awaitTimer: DispatchSourceTimer? // 新增:等待音效超时定时器 private let logger = OSLog(subsystem: "com.yunqiinnovation.agent_service", category: "AudioPlayer") private let audioQueue = DispatchQueue(label: "agent.audio.player", qos: .userInitiated) + + // 等待音效超时时间(秒)- 改为 var 以支持动态修改 + private var awaitTimeoutSeconds: TimeInterval = 10.0 private var resourcesBundle: Bundle { #if SWIFT_PACKAGE @@ -1247,7 +1251,14 @@ class AudioPlayer { } func playAwaitSound() { + // 先停止之前的等待音效和定时器 + stopAwaitSound() + + // 播放等待音效 playLoopSound(named: "await", fileType: "mp3", player: &awaitPlayer) + + // 启动超时定时器 + startAwaitTimeout() } func playStopSound() { @@ -1264,8 +1275,14 @@ class AudioPlayer { } func stopAwaitSound() { + // 停止音频播放 awaitPlayer?.stop() awaitPlayer = nil + + // 停止超时定时器 + stopAwaitTimeout() + + os_log("停止等待音效播放", log: logger, type: .debug) } private func playSound(named: String, fileType: String = "mp3") { @@ -1338,6 +1355,41 @@ class AudioPlayer { os_log("播放循环音频失败: %{public}@", log: logger, type: .error, error.localizedDescription) } } + + private func startAwaitTimeout() { + // 停止之前的定时器 + stopAwaitTimeout() + + // 创建新的定时器 + let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global(qos: .utility)) + awaitTimer = timer + + timer.schedule(deadline: .now() + awaitTimeoutSeconds) + timer.setEventHandler { [weak self] in + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + + os_log("等待音效超时,自动停止播放 - 超时时间: %.0f秒", + log: self.logger, type: .info, self.awaitTimeoutSeconds) + + self.stopAwaitSound() + } + } + timer.resume() + + os_log("启动等待音效超时定时器,超时时间: %.0f秒", + log: logger, type: .debug, awaitTimeoutSeconds) + } + + private func stopAwaitTimeout() { + awaitTimer?.cancel() + awaitTimer = nil + } + + func setAwaitTimeout(_ seconds: TimeInterval) { + awaitTimeoutSeconds = max(2.0, seconds) // 最小5秒 + os_log("设置等待音效超时时间: %.0f秒", log: logger, type: .info, awaitTimeoutSeconds) + } } extension AgentServiceImpl: AzureAsrHelper.ContinuousRecognizeCallback {