First, you must specify the AVAudioSession sound behavior of your application. Apple called this category an audio session; it can be set using
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
For example, AVAudioSessionCategoryPlayback :
When using this category, the sound of your application continues with the Silent switch turned off or the screen turned off. (The switch is called the Ring / Silent switch on the iPhone.)
This category generally prohibits mixing sound from other applications using an audio application. To enable mixing for this category, use the kAudioSessionProperty_OverrideCategoryMixWithOthers property.
Then, as soon as the audio recording is installed, the application will respond to some sound notifications, such as AVAudioSessionInterruptionNotification or AVAudioSessionRouteChangeNotification
To answer the original question, AVAudioSessionRouteChangeNotification is called when the audio route has been changed (for example, the plug-in / plug-in for the headset, and the Bluetooth device is also turned off). With a little code, we can find the reason for the route change. And, in our case, start the player again, and the headset is turned off.
- (void)viewDidLoad { [super viewDidLoad]; NSError *setCategoryErr; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
In conclusion, also think about the user in a boring meeting who accidentally connected his headset. He did not have such behavior that would make the device scream loudly in the room!
source share