I am using AVPlayer
for music playback. My problem is that after an incoming call, the player won\'t resume. How do I handle this when an incoming call comes?>
AVAudioSession will send a notification when an interruption starts and ends. See Handling Audio Interruptions
- (id)init
{
if (self = [super init]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
}
}
- (void)audioSessionInterrupted:(NSNotification *)notification
{
int interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
if (_state == GBPlayerStateBuffering || _state == GBPlayerStatePlaying) {
NSLog(@"Pausing for audio session interruption");
pausedForAudioSessionInterruption = YES;
[self pause];
}
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
if ([notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue] == AVAudioSessionInterruptionOptionShouldResume) {
if (pausedForAudioSessionInterruption) {
NSLog(@"Resuming after audio session interruption");
[self play];
}
}
pausedForAudioSessionInterruption = NO;
}
}