欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

swift 音频被中断处理

程序员文章站 2022-06-23 14:18:48
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