I'm working on an audio streaming app and I'm having troubles with interruptions. I have read that I should use AVAudioSessionInterruptionNotification, but my problem is it's getting called only when the interruption starts and not when it ends. Here's my code.
static dispatch_once_t pred;
static Player *sharedAudioPlayer = nil;
dispatch_once(&pred, ^
{
sharedAudioPlayer = [[self alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:sharedAudioPlayer selector:@selector(playerItemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedAudioPlayer
selector:@selector(audioSessionInterrupted:)
name:AVAudioSessionInterruptionNotification
object:audioSession];
});
and then
- (void)audioSessionInterrupted:(NSNotification*)notification {
NSDictionary *interruptionDictionary = [notification userInfo];
NSNumber *interruptionType = (NSNumber *)[interruptionDictionary valueForKey:AVAudioSessionInterruptionTypeKey];
if ([interruptionType intValue] == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Interruption started");
if (self.delegate && [self.delegate respondsToSelector:@selector(playerWasPaused)]) {
[self.delegate playerWasPaused];
}
} else if ([interruptionType intValue] == AVAudioSessionInterruptionTypeEnded){
NSLog(@"Interruption ended");
[player play];
} else {
NSLog(@"Something else happened");
}
When a call starts I get printed twice "Interruption started", but when it ends I don't get anything. I even tried to check if something else is sent to the notification but still nothing. Any ideas?