Browse Source

fix(AudioSessionHub): 修复音频中断处理逻辑并优化通知发送

修复中断开始时的状态切换逻辑,避免重复发送中断开始通知
将中断结束通知移至正确的位置处理,并移除重复的通知发送代码
优化日志输出格式,增加中断状态信息
newdev_shunjiawei
liwei1dao 12 months ago
parent
commit
b831eefa24
  1. 44
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AudioSessionHub.swift

44
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)

Loading…
Cancel
Save