mirror of https://github.com/axmolengine/axmol.git
Merge pull request #12106 from WenhaiLin/v3-audio-ios-phonecall
AudioEngine:Fixed audio can not resume if it is interrupted cause by an incoming phone call.
This commit is contained in:
commit
d7534c26a3
|
@ -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];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue