My app is playing in background well. Pause and play also works well even my app is background using the methods registered for AVAudioSessionInterruptionNotification
.
The problem arrives steps for the scenario:
- Launch my app -> goes background music from my app playing well
- launch apple's music app and play. AVAudioSessionInterruptionNotification fired.
- When i pause the music app or kill the music app. AVAudioSessionInterruptionNotification doesn't get fired
Code: I am doing this in appdelegate. didfinishlaunching
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error: nil];
self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"http://someurl.com:8040/;stream.mp3"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
- (void) onAudioSessionEvent: (NSNotification *) notification
{
//Check the type of notification, especially if you are sending multiple AVAudioSession events here
NSLog(@"Interruption notification name %@", notification.name);
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption notification received %@!", notification);
//Check to see if it was a Begin interruption
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"Interruption began!");
} else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
NSLog(@"Interruption ended!");
//Resume your audio
NSLog(@"Player status %i", self.player.status);
// Resume playing the audio.
[self.player play];
}
}
}