• 【iOS】音频中断


    【iOS】音频中断

    持有AVAudioSession的类添加AVAudioSessionInterruptionNotification监听音频中断状态

    (电话、闹铃等归结为一般性的中断,由AVAudioSessionInterruptionNotification通知)

    监听回调中userInfo包含AVAudioSessionInterruptionTypeKeykey所对应的枚举值分别为:

    /// Values for AVAudioSessionInterruptionTypeKey in AVAudioSessionInterruptionNotification's
    /// userInfo dictionary.
    typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType) {
        /// 中断开始
        AVAudioSessionInterruptionTypeBegan = 1, ///< the system has interrupted your audio session
        /// 中断结束
        AVAudioSessionInterruptionTypeEnded = 0, ///< the interruption has ended
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    持有AVAudioSession的类添加AVAudioSessionSilenceSecondaryAudioHintNotification监听音频中断状态

    (其他App占用session 导致中断,由AVAudioSessionSilenceSecondaryAudioHintNotification通知)

    监听回调中userInfo包含AVAudioSessionSilenceSecondaryAudioHintTypeKey,key所对应的枚举值分别为:

    /// Values for AVAudioSessionSilenceSecondaryAudioHintTypeKey in
    /// AVAudioSessionSilenceSecondaryAudioHintNotification's userInfo dictionary, to indicate whether
    /// optional secondary audio muting should begin or end.
    typedef NS_ENUM(NSUInteger, AVAudioSessionSilenceSecondaryAudioHintType) {
    	/// 其他App占用session
        /// Another application's primary audio has started.
        AVAudioSessionSilenceSecondaryAudioHintTypeBegin = 1,
    
    	/// 其他App释放session
        /// Another application's primary audio has stopped.
        AVAudioSessionSilenceSecondaryAudioHintTypeEnd = 0,
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    持有AVAudioSession的类添加AVAudioSessionRouteChangeNotification监听

    (外设改变,由AVAudioSessionRouteChangeNotification通知)

    监听回调中userInfo包含AVAudioSessionRouteChangeReasonKey,key所对应的枚举值分别为:

    /// Values for AVAudioSessionRouteChangeReasonKey in AVAudioSessionRouteChangeNotification's
    /// userInfo dictionary
    typedef NS_ENUM(NSUInteger, AVAudioSessionRouteChangeReason) {
        
        /// 未知原因
        /// The reason is unknown.
        AVAudioSessionRouteChangeReasonUnknown = 0,
    	 有新设备可用
        /// A new device became available (e.g. headphones have been plugged in).
        AVAudioSessionRouteChangeReasonNewDeviceAvailable = 1,
    
    	/// 旧设备不可用
        /// The old device became unavailable (e.g. headphones have been unplugged).
        AVAudioSessionRouteChangeReasonOldDeviceUnavailable = 2,
    
    	/// category改变
        /// The audio category has changed (e.g. AVAudioSessionCategoryPlayback has been changed to
        /// AVAudioSessionCategoryPlayAndRecord).
        AVAudioSessionRouteChangeReasonCategoryChange = 3,
    
    	/// App重置了输出设置
        /// The route has been overridden (e.g. category is AVAudioSessionCategoryPlayAndRecord and
        /// the output has been changed from the receiver, which is the default, to the speaker).
        AVAudioSessionRouteChangeReasonOverride = 4,
    
    	/// 从睡眠中唤醒
        /// The device woke from sleep.
        AVAudioSessionRouteChangeReasonWakeFromSleep = 6,
    
    	/// 当前category下没有可用的设备
        /// Returned when there is no route for the current category (for instance, the category is
        /// AVAudioSessionCategoryRecord but no input device is available).
        AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory = 7,
    
    	/// Router的配置改变
        /// Indicates that the set of input and/our output ports has not changed, but some aspect of
        /// their configuration has changed.  For example, a port's selected data source has changed.
        /// (Introduced in iOS 7.0, watchOS 2.0, tvOS 9.0).
        AVAudioSessionRouteChangeReasonRouteConfigurationChange = 8
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    不同的音频播放类,中断处理不同

    System Sound Services: 系统自动处理,中断开始:静音,中断结束:继续播放

    AVFoundation: AVAudioPlayer/AVAudioRecord在中断开始时会自动暂停,需记录暂停位置、时间等信息,中断结束后需要调用播放方法并设置时间为上次暂停的时间才能继续播放。

    AVAudioSession下的其他监听

    AVAudioSessionMediaServicesWereLostNotification 监听媒体服务器丢失/连接不上

    AVAudioSessionMediaServicesWereResetNotification 监听媒体服务器重启

    注意点

    1、 有中断开始的通知,不一定有中断结束的通知,需要监听App重新进入前台的状态

    2、不论什么方式播放音频,一定是电话优先,来电话的情况下其他音频中断

    3、AVAudioSession可监听外设音频状态(例如耳机)

  • 相关阅读:
    微服务篇之限流
    面试又挂了:大厂面试到底更看重学历还是技术?来看看大佬的说法
    MacOS Sonoma14.2.1系统SSH免密登录
    el-dialog弹窗拖动
    吃透Chisel语言.20.Chisel组合电路(二)——Chisel编码器与解码器实现
    MYSQL sql的技巧与避坑
    JUC学习笔记——共享模型之不可变
    男孩姓岳取什么名字好听
    如何安装 Elasticsearch
    Python3.11教程5:类与对象
  • 原文地址:https://blog.csdn.net/weixin_43874301/article/details/126219574