Starting from iOS 6 you must handle AVAudioSessionInterruptionNotification
and AVAudioSessionMediaServicesWereResetNotification
, before this you had to use delegate methods.
First you should call the AVAudioSession singleton and configure it for your desired use.
For example:
AVAudioSession *aSession = [AVAudioSession sharedInstance];
[aSession setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionAllowBluetooth
error:&error];
[aSession setMode:AVAudioSessionModeDefault error:&error];
[aSession setActive: YES error: &error];
Then you should implement two methods, for the notifications which the AVAudioSession would call:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAudioSessionInterruption:)
name:AVAudioSessionInterruptionNotification
object:aSession];
First one is for any interruption which would be called because of an incoming call, alarm clock, etc.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleMediaServicesReset)
name:AVAudioSessionMediaServicesWereResetNotification
object:aSession];
The second one if the media server resets for any reason, you should handle this notification to reconfigure audio or do any housekeeping. By the way the notification dictionary won't contain any object.
Here is an example for handling the playback interruption:
- (void)handleAudioSessionInterruption:(NSNotification*)notification {
NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];
switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
// ? Audio has stopped, already inactive
// ? Change state of UI, etc., to reflect non-playing state
} break;
case AVAudioSessionInterruptionTypeEnded:{
// ? Make session active
// ? Update user interface
// ? AVAudioSessionInterruptionOptionShouldResume option
if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
// Here you should continue playback.
[player play];
}
} break;
default:
break;
}
}
Notice that you should resume playback when the optional value is AVAudioSessionInterruptionOptionShouldResume
And for the other notification you should take care of the following:
- (void)handleMediaServicesReset {
// ? No userInfo dictionary for this notification
// ? Audio streaming objects are invalidated (zombies)
// ? Handle this notification by fully reconfiguring audio
}
Regards.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…