I have an app that plays music using AVAudioPlayer. When the app is in the background and there is an incoming FaceTime call, I receive the AVAudioSessionInterruptionNotification to pause the player. When I then end the FaceTime call, I get the AVAudioSessionInterruptionNotification again. However, when I call 'play' on the AVAudioPlayer, I don't hear any audio starting. When the same happens on a phone call, or an *outgoing* FaceTime call the audio player resumes playing perfectly fine. I've added my code below. Does anybody know what I'm doing wrong, or whether this is a bug? The same codes works fine on iOS 8.
// Setup Audio Session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
[audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil];
// Setup Audio Player
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.songURL error:nil];
if ([self.audioPlayer prepareToPlay]) {
[self.audioPlayer play];
}
...
- (void)interruption:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
if ([[userInfo objectForKey:AVAudioSessionInterruptionTypeKey] integerValue] == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Audio session was interrupted");
[self.audioPlayer pause];
} else if ([[userInfo objectForKey:AVAudioSessionInterruptionTypeKey] integerValue] == AVAudioSessionInterruptionTypeEnded) {
NSLog(@"Audio interruption ended");
if ([self.audioPlayer prepareToPlay]) {
NSLog(@"Will resume playing audio");
[self.audioPlayer play];
} else {
NSLog(@"Failed to prepare to play");
}
}
}