(电话、闹铃等归结为一般性的中断,由AVAudioSessionInterruptionNotification通知)
监听回调中userInfo包含AVAudioSessionInterruptionTypeKey,key所对应的枚举值分别为:
/// 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
};
(其他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,
};
(外设改变,由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
};
System Sound Services: 系统自动处理,中断开始:静音,中断结束:继续播放
AVFoundation: AVAudioPlayer/AVAudioRecord在中断开始时会自动暂停,需记录暂停位置、时间等信息,中断结束后需要调用播放方法并设置时间为上次暂停的时间才能继续播放。
AVAudioSessionMediaServicesWereLostNotification 监听媒体服务器丢失/连接不上
AVAudioSessionMediaServicesWereResetNotification 监听媒体服务器重启
1、 有中断开始的通知,不一定有中断结束的通知,需要监听App重新进入前台的状态
2、不论什么方式播放音频,一定是电话优先,来电话的情况下其他音频中断
3、AVAudioSession可监听外设音频状态(例如耳机)