iOS 音视频开发 - 系统中断音频(Swift语言)

一、分析

注册AVAudioSession.interruptionNotification的通知,可以收到播放打断通知。系统将此通知发布到主线程。

从iOS 10开始,系统会在暂停App进程时停用App的音频会话。
当App再次开始运行时,它会收到一个中断通知,表示系统已停用其音频会话。
此通知最终会延迟收到,因为系统只能在App再次运行时发送。
如果系统因此暂停了App的音频会话,则userinfo包含值为true的AvaudioSessionInterruptionWasSuspendKey。
假如音频会话配置是不可混合的(playback、playAndRecord、soloAmbient和multiRoute类别的默认行为),进入后台时未使用音频,请停用音频会话。
这样做可以避免系统停用您的音频会话(并接收到这种有点混乱的通知)。

其中userinfo有如下字段:

1. AvaudioSessionInterruptionTypeKey:

分began与end,用来表示打断开始与打断结束。

2. AVAudioSessionInterruptionOptionKey:

只在打断结束时返回。选项用shouldResume来指示:另一个音频会话的中断已结束,应用程序可以恢复其音频会话。

3. AVAudioSessionInterruptionWasSuspendedKey:

该属性只在打断开始时返回。存在于版本iOS 10.3-14.5。
用于确定这次中断是否由于系统挂起App所致。
获取到的是一个NSNumber表示的Bool。为true表示中断是由于系统挂起,false是被另一音频打断。

4. AVAudioSessionInterruptionReasonKey:

该属性只在打断开始时返回。存在于版本iOS 14.5以后,用来代替AVAudioSessionInterruptionWasSuspendedKey
default:因为另一个音频会话被激活,音频中断。(例如后台播着音频,此时播放微信语音)
appWasSuspended:由于APP被系统挂起,音频中断
builtInMicMuted:音频因内置麦克风静音而中断(例如iPad智能关闭套【iPad's Smart Folio】合上)

二、代码

//注册播放打断通知
NotificationCenter.default.addObserver(self, selector:#selector(interruptionTypeChanged(_:)), name: AVAudioSession.interruptionNotification, object: nil)
///打断
///打断
@objc private func interruptionTypeChanged(_ nof:NSNotification) {
    guard let userInfo = nof.userInfo, let reasonValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt else { return }
    
    switch reasonValue {
    case AVAudioSession.InterruptionType.began.rawValue://Began
        var isAnotherAudioSuspend = false //是否是被其他音频会话打断
        if #available(iOS 10.3, *) {
            if #available(iOS 14.5, *) {
                // iOS 14.5之后使用InterruptionReasonKey
                let reasonKey = userInfo[AVAudioSessionInterruptionReasonKey] as! UInt
                switch reasonKey {
                case AVAudioSession.InterruptionReason.default.rawValue:
                    //因为另一个会话被激活,音频中断
                    isAnotherAudioSuspend = true
                    break
                case AVAudioSession.InterruptionReason.appWasSuspended.rawValue:
                    //由于APP被系统挂起,音频中断。
                    break
                case AVAudioSession.InterruptionReason.builtInMicMuted.rawValue:
                    //音频因内置麦克风静音而中断(例如iPad智能关闭套iPad's Smart Folio关闭)
                    break
                default: break
                }
                print(reasonKey)
            } else {
                // iOS 10.3-14.5,InterruptionWasSuspendedKey为true表示中断是由于系统挂起,false是被另一音频打断
                let suspendedNumber:NSNumber = userInfo[AVAudioSessionInterruptionWasSuspendedKey] as! NSNumber
                isAnotherAudioSuspend = !suspendedNumber.boolValue
            }
        }
        
        if isAnotherAudioSuspend {
            if (self.delegate != nil){
                self.delegate?.mediaChangeInterruptionType(begin: true)
            }
        }
        break
    case AVAudioSession.InterruptionType.ended.rawValue://End
        let optionKey = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt
        if optionKey == AVAudioSession.InterruptionOptions.shouldResume.rawValue {
            //指示另一个音频会话的中断已结束,本应用程序可以恢复音频。
            if (self.delegate != nil){
                self.delegate?.mediaChangeInterruptionType(begin: false)
            }
        }
        break
    default: break
    }
}

你可能感兴趣的:(iOS 音视频开发 - 系统中断音频(Swift语言))