AudioEngine:Fixed audio can not resume if it is interrupted cause by an incoming phone call.

This commit is contained in:
WenhaiLin 2015-05-29 21:41:54 +08:00
parent 839e7fbb59
commit 59435f8749
1 changed files with 25 additions and 4 deletions

View File

@ -162,6 +162,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio
{ {
if ([[[UIDevice currentDevice] systemVersion] intValue] > 5) { if ([[[UIDevice currentDevice] systemVersion] intValue] > 5) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:UIApplicationDidBecomeActiveNotification object:[AVAudioSession sharedInstance]];
} }
else { else {
AudioSessionInitialize(NULL, NULL, AudioEngineInterruptionListenerCallback, self); AudioSessionInitialize(NULL, NULL, AudioEngineInterruptionListenerCallback, self);
@ -172,6 +173,8 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio
-(void)handleInterruption:(NSNotification*)notification -(void)handleInterruption:(NSNotification*)notification
{ {
static bool resumeOnBecomingActive = false;
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) { if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue]; NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
if (reason == AVAudioSessionInterruptionTypeBegan) { if (reason == AVAudioSessionInterruptionTypeBegan) {
@ -179,17 +182,35 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio
} }
if (reason == AVAudioSessionInterruptionTypeEnded) { if (reason == AVAudioSessionInterruptionTypeEnded) {
OSStatus result = AudioSessionSetActive(true); if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
if (result) NSLog(@"Error setting audio session active! %d\n", result); NSError *error = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
alcMakeContextCurrent(s_ALContext); alcMakeContextCurrent(s_ALContext);
} else {
resumeOnBecomingActive = true;
} }
} }
}
if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification] && resumeOnBecomingActive) {
resumeOnBecomingActive = false;
NSError *error = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
error: &error];
if (!success) {
printf("Fail to set audio session.\n");
return;
}
[[AVAudioSession sharedInstance] setActive:YES error:&error];
alcMakeContextCurrent(s_ALContext);
}
} }
-(void) dealloc -(void) dealloc
{ {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[super dealloc]; [super dealloc];
} }