当前位置: 移动技术网 > 移动技术>移动开发>IOS > swift 音频被中断处理

swift 音频被中断处理

2020年09月28日  | 移动技术网移动技术  | 我要评论
1、监听音频打断事件NotificationCenter.default.addObserver(self, selector: #selector(audioStart(_:)), name: NSNotification.Name.AVAudioSessionInterruption, object: nil)2、在通知方法中处理音频中断事件AVAudioSessionInterruptionType有两种类型:began 和 endedpublic enum Interruptio.
  • 1、监听音频打断事件
NotificationCenter.default.addObserver(self, selector: #selector(audioStart(_:)), name:  NSNotification.Name.AVAudioSessionInterruption, object: nil)
  • 2、在通知方法中处理音频中断事件
    AVAudioSessionInterruptionType有两种类型:beganended
public enum InterruptionType : UInt {

        case began = 1

        case ended = 0
    }
  • began表示收到中断事件开始的通知
  • ended表示收到中断事件结束的通知
@objc private func audioStart(_ note: Notification){
        print("addInterruptionSession \(note) \(note.userInfo![AVAudioSessionInterruptionTypeKey])")
        
        if AVAudioSessionInterruptionType.began.rawValue == note.userInfo![AVAudioSessionInterruptionTypeKey] as? UInt{
            print("addInterruptionSession 收到音频中断开始通知")
            //暂停音频

        } else if AVAudioSessionInterruptionType.ended.rawValue == note.userInfo![AVAudioSessionInterruptionTypeKey]as? UInt{
            print("addInterruptionSession 收到音频中断结束通知")
            //恢复音频
        }
    }

本文地址:https://blog.csdn.net/lin1109221208/article/details/108852992

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网