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)