iOS AVAudioSessionInterruptionNotification receive notifications confusion

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/shengpeng3344/article/details/83617979

iOS AVAudioSessionInterruptionNotification receive notifications confusion

Problem: Audio break into the foreground receive notification

When processing AVAudioSessionInterruptionNotification notice, each retreated to the background, then retreated to the front desk, subject to a AVAudioSessionInterruptionNotification notice, and AVAudioSessionInterruptionTypeKey
is AVAudioSessionInterruptionTypeBegan, said he was surprised, I did not interrupt the audio ah, no phone call came in. Rear with official documentation: 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).

From ios10 started, the system will turn off the audio session for most applications in response to the application process is suspended. When the application starts running again, it receives an interrupt notification that its audio system session has been disabled. This delay is bound to notice, because it can only send in an application running again . If your application's audio session therefore suspended, userInfo dictionary will contain AVAudioSessionInterruptionWasSuspendedKey value is YES key.

Solution: Disable audio into the background

On this reason, when it so into the background, if not the background player (fields can infoplist determination), the audio session into a non-active

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

Back to the front desk and then set to YES

Guess you like

Origin blog.csdn.net/shengpeng3344/article/details/83617979