From e9c4e7879f1d9d50ba09f2986e9161642e06c8ce Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Thu, 30 Oct 2025 20:42:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(AudioSessionHub):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=9F=B3=E9=A2=91=E4=B8=AD=E6=96=AD=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E4=BC=98=E5=8C=96=E9=80=9A=E7=9F=A5=E5=8F=91?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复中断开始时的状态切换逻辑,避免重复发送中断开始通知 将中断结束通知移至正确的位置处理,并移除重复的通知发送代码 优化日志输出格式,增加中断状态信息 --- .../agent_service/AudioSessionHub.swift | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AudioSessionHub.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AudioSessionHub.swift index c0364b79c..68332e129 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AudioSessionHub.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AudioSessionHub.swift @@ -455,8 +455,7 @@ public class AudioSessionHub { /// 处理音频中断 @objc private func handleAudioSessionInterruption(notification: Notification) { - os_log("🔊 收到音频会话中断通知", log: logger, type: .info) - + os_log("🔔 收到音频会话中断通知 userInfo:", notification.userInfo ?? [:]) guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { @@ -467,17 +466,25 @@ public class AudioSessionHub { switch type { case .began: // 中断开始 - isInterrupted = true - os_log("🚫 音频会话中断开始", log: logger, type: .info) - os_log(" - 中断状态: %{public}@", log: logger, type: .info, isInterrupted ? "是" : "否") - - // 发送中断开始通知给其他服务 - NotificationCenter.default.post( - name: NSNotification.Name("AudioSessionInterruptionBegan"), - object: nil, - userInfo: ["type": "began"] - ) - + isInterrupted = !isInterrupted + os_log("🚫 音频会话中断: %{public}@", log: logger, type: .info, isInterrupted ? "开始" : "结束") + if (isInterrupted){ + // 发送中断开始通知给其他服务 + NotificationCenter.default.post( + name: NSNotification.Name("AudioSessionInterruptionBegan"), + object: nil, + userInfo: ["type": "began"] + ) + }else{ + // 发送中断结束通知给其他服务,包含恢复建议 + NotificationCenter.default.post( + name: NSNotification.Name("AudioSessionInterruptionEnded"), + object: nil, + userInfo: [ + "type": "ended" + ] + ) + } case .ended: // 中断结束 isInterrupted = false @@ -586,17 +593,6 @@ public class AudioSessionHub { os_log("🔧 音频类别已变化", log: logger, type: .info) os_log(" - 当前类别: %{public}@", log: logger, type: .info, audioSession.category.rawValue) os_log(" - 当前模式: %{public}@", log: logger, type: .info, audioSession.mode.rawValue) - - // 发送中断结束通知给其他服务,包含恢复建议 - NotificationCenter.default.post( - name: NSNotification.Name("AudioSessionInterruptionEnded"), - object: nil, - userInfo: [ - "type": "ended", - "shouldResume": false, - "options": 0 - ] - ) case .override: os_log("🔀 音频路由被覆盖", log: logger, type: .info)