iOS AVAudioSessionInterruptionNotification通知接收混乱

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shengpeng3344/article/details/83617979

iOS AVAudioSessionInterruptionNotification通知接收混乱

问题:进入前台收到音频打断通知

处理AVAudioSessionInterruptionNotification通知时,每次退到后台,然后退到前台,会受到一个AVAudioSessionInterruptionNotification通知,且AVAudioSessionInterruptionTypeKey
为AVAudioSessionInterruptionTypeBegan,表示很诧异,我并没有音频打断啊,没有电话call进来。后面查了官方文档说明:https://developer.apple.com/documentation/avfoundation/avaudiosessioninterruptionnotification

If your audio session is configured to be non-mixable (the default behavior for the AVAudioSessionCategoryPlayback, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategorySoloAmbient, and AVAudioSessionCategoryMultiRoute categories), it's recommended that you deactivate your audio session if you're not actively using audio when you go into the background. Doing so will avoid having your audio session deactivated by the system (and receiving this somewhat confusing notification).

Note

Starting in iOS 10, the system will deactivate the audio session of most apps in response to the app process being suspended. When the app starts running again, it will receive an interruption notification that its audio session has been deactivated by the system. This notification is necessarily delayed in time because it can only be delivered once the app is running again. If your app’s audio session was suspended for this reason, the userInfo dictionary will contain the AVAudioSessionInterruptionWasSuspendedKey key with a value of YES.

If your audio session is configured to be non-mixable (the default behavior for the AVAudioSessionCategoryPlayback, AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategorySoloAmbient, and AVAudioSessionCategoryMultiRoute categories), it’s recommended that you deactivate your audio session if you’re not actively using audio when you go into the background. Doing so will avoid having your audio session deactivated by the system (and receiving this somewhat confusing notification).

从ios10开始,系统将关闭大多数应用程序的音频会话,以响应应用程序进程被暂停。当应用程序重新开始运行时,它将收到一个中断通知,提示它的音频会话已被系统停用。这个通知必然会延迟,因为它只能在应用程序再次运行时发送。如果您的应用程序的音频会话因此暂停,userInfo字典将包含AVAudioSessionInterruptionWasSuspendedKey值为YES的键。

解决:进入后台停用音频

就这个原因,故所以在进入后台时,如果不是后台播放(可以用infoplist字段判断),将音频session置为非active

NSError *error = nil; [[AVAudioSession sharedInstance] setActive:NO error:&error];

回到前台再置为YES

猜你喜欢

转载自blog.csdn.net/shengpeng3344/article/details/83617979
今日推荐