|
|
|
@ -470,6 +470,13 @@ public class AudioSessionHub { |
|
|
|
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"] |
|
|
|
) |
|
|
|
|
|
|
|
case .ended: |
|
|
|
// 中断结束 |
|
|
|
isInterrupted = false |
|
|
|
@ -480,16 +487,43 @@ public class AudioSessionHub { |
|
|
|
let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue) |
|
|
|
os_log("中断选项: %d, 应该恢复: %{public}@", log: logger, type: .info, optionsValue, options.contains(.shouldResume) ? "是" : "否") |
|
|
|
|
|
|
|
if options.contains(.shouldResume), let intent = currentIntent { |
|
|
|
os_log("恢复之前的音频意图: %{public}@", log: logger, type: .info, String(describing: intent)) |
|
|
|
// 恢复之前的音频意图 |
|
|
|
//begin(intent) |
|
|
|
os_log("✓ 音频会话中断结束,已恢复", log: logger, type: .info) |
|
|
|
// 发送中断结束通知给其他服务,包含恢复建议 |
|
|
|
NotificationCenter.default.post( |
|
|
|
name: NSNotification.Name("AudioSessionInterruptionEnded"), |
|
|
|
object: nil, |
|
|
|
userInfo: [ |
|
|
|
"type": "ended", |
|
|
|
"shouldResume": options.contains(.shouldResume), |
|
|
|
"options": optionsValue |
|
|
|
] |
|
|
|
) |
|
|
|
|
|
|
|
if options.contains(.shouldResume) { |
|
|
|
os_log("系统建议恢复音频会话", log: logger, type: .info) |
|
|
|
|
|
|
|
// 延迟一段时间后发送恢复通知,让MusicService处理音频会话设置 |
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { |
|
|
|
os_log("音频中断结束,发送恢复通知", log: self.logger, type: .info) |
|
|
|
|
|
|
|
// 直接发送恢复播放通知,让MusicService自己处理音频会话 |
|
|
|
self.resumeMusicServiceIfNeeded() |
|
|
|
} |
|
|
|
} else { |
|
|
|
os_log("不需要恢复音频会话", log: logger, type: .info) |
|
|
|
} |
|
|
|
} else { |
|
|
|
os_log("⚠️ 中断通知中无选项信息", log: logger, type: .default) |
|
|
|
|
|
|
|
// 即使没有选项信息,也发送通知 |
|
|
|
NotificationCenter.default.post( |
|
|
|
name: NSNotification.Name("AudioSessionInterruptionEnded"), |
|
|
|
object: nil, |
|
|
|
userInfo: [ |
|
|
|
"type": "ended", |
|
|
|
"shouldResume": false, |
|
|
|
"options": 0 |
|
|
|
] |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
@unknown default: |
|
|
|
@ -736,6 +770,22 @@ public class AudioSessionHub { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 尝试恢复MusicService播放 |
|
|
|
private func resumeMusicServiceIfNeeded() { |
|
|
|
os_log("🎵 [AudioSessionHub] 开始恢复MusicService播放流程", log: logger, type: .info) |
|
|
|
|
|
|
|
// 不在AudioSessionHub中设置音频会话类别,让MusicService自己处理 |
|
|
|
// 直接发送恢复播放的通知,让MusicService处理音频会话设置 |
|
|
|
DispatchQueue.main.async { |
|
|
|
NotificationCenter.default.post( |
|
|
|
name: NSNotification.Name("AudioSessionHub_ResumeMusicPlayback"), |
|
|
|
object: nil, |
|
|
|
userInfo: ["shouldResume": true] |
|
|
|
) |
|
|
|
os_log("🎵 [AudioSessionHub] 已发送恢复播放通知", log: self.logger, type: .info) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
deinit { |
|
|
|
NotificationCenter.default.removeObserver(self) |
|
|
|
} |
|
|
|
|