微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何快速找到哪个应用程序中断了音频会话

如何解决如何快速找到哪个应用程序中断了音频会话

我正在开发音频播放器应用。

我使用 AVAudioSession 来处理 AVPlayer 播放会话和中断。我收到中断通知,并能够基于此暂停/播放音频

我想获取的只是出于分析目的而中断音频的原因。

这是我用来配置 AVAudioSession 和处理音频中断的代码

  1. AVAudioSession 配置
// AudioSession Configuration
let audioSession = AVAudioSession.sharedInstance()
do {
    try? audioSession.setCategory(AVAudioSession.Category.playback,options: .allowAirPlay)
}
catch let error as NSError {
    print("Setting category to AVAudioSessionCategoryPlayback Failed: \(error)")
}
try? audioSession.setActive(true)
  1. AVAudioSession 中断通知添加观察者:
let notificationCenter = NotificationCenter.default
        notificationCenter.addobserver(self,selector: #selector(handleInterruption),name: AVAudioSession.interruptionNotification,object: nil)
  1. AVAudioSession 中断处理通知
@objc private func handleInterruption(notification: Notification) {
        guard let userInfo = notification.userInfo,let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
                return
        }
        
        switch type {
        case .began:
            dispatchQueue.main.async {
                self.pause(pauseReason: .interruption)
                print("handleInterruption :- Pause")
            }
        case .ended:
            guard let optionsValue = userInfo[AVAudioSessionInterruptionoptionKey] as? UInt else { return }
            let options = AVAudioSession.Interruptionoptions(rawValue: optionsValue)
            if options.contains(.shouldResume) {
                // Interruption ended. Playback should resume.
                dispatchQueue.main.asyncAfter(deadline: .Now() + 2) {
                    self.play(isResume: true)
                    print("handleInterruption :- Play")
                }
            }
            else {
                // Interruption ended. Playback should not resume.
                self.pause(pauseReason: .interruption)
                print("handleInterruption :- Pause")
            }
        @unkNown default:
            break
        }
    }

是否有任何函数/委托/通知可以帮助我找到音频播放中断的原因?

我想获得音频播放中断的原因

  1. 其他音乐应用(如 Gaana、Wynk)
  2. 电话/短信等

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。