小游戏运行如何与企业数字化转型息息相关
1864
2022-09-30
【iOS】音频中断
【iOS】音频中断
持有AVAudioSession的类添加AVAudioSessionInterruptionNotification监听音频中断状态
(电话、闹铃等归结为一般性的中断,由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};
持有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,};
持有AVAudioSession的类添加AVAudioSessionRouteChangeNotification监听
(外设改变,由AVAudioSessionRouteChangeNotification通知)
监听回调中userInfo包含AVAudioSessionRouteChangeReasonKey,key所对应的枚举值分别为:
/// Values for AVAudioSessionRouteChangeReasonKey in AVAudioSessionRouteChangeNotification's/// userInfo dictionarytypedef 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在中断开始时会自动暂停,需记录暂停位置、时间等信息,中断结束后需要调用播放方法并设置时间为上次暂停的时间才能继续播放。
AVAudioSession下的其他监听
AVAudioSessionMediaServicesWereLostNotification 监听媒体服务器丢失/连接不上
AVAudioSessionMediaServicesWereResetNotification 监听媒体服务器重启
注意点
1、 有中断开始的通知,不一定有中断结束的通知,需要监听App重新进入前台的状态
2、不论什么方式播放音频,一定是电话优先,来电话的情况下其他音频中断
3、AVAudioSession可监听外设音频状态(例如耳机)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~