Skip to content

Commit

Permalink
Fix crash after dismissing a phone call
Browse files Browse the repository at this point in the history
  • Loading branch information
dapetcu21 committed Sep 10, 2019
1 parent 05f0b7d commit af985c1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions bridge/src/fmod_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,62 @@

#import <AVFoundation/AVFoundation.h>

#define AUDIO_RESUME_RETRY_COUNT 20
#define AUDIO_RESUME_RETRY_INTERVAL 0.2

static NSTimer * resumeTimer = nil;

// In case it doesn't work from the first try, retry activating the audio session
// a few times. For example, after dismissing a phone call, [AVAudioSession setActive: error:]
// errors out from the first try, but works on the second
static void resumeAudioSession(int retries) {
@autoreleasepool {
NSError * error;
BOOL success = [[AVAudioSession sharedInstance] setActive:TRUE error: &error];
if (success) {
FMODBridge_resumeMixer();
return;
}

if (error) {
LOGW("Cannot activate audio session: %s", [[error localizedDescription] UTF8String]);
} else {
LOGW("Cannot activate audio session and no error was issued");
}

if (retries <= 0) {
LOGE("Cannot activate audio session. Not retrying anymore.");
return;
}

resumeTimer = [NSTimer
scheduledTimerWithTimeInterval: AUDIO_RESUME_RETRY_INTERVAL
repeats: NO
block: ^(NSTimer *timer) {
resumeTimer = nil;
resumeAudioSession(retries - 1);
}
];
}
}

void FMODBridge_initIOSInterruptionHandler() {
@autoreleasepool {
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification object:nil queue:nil usingBlock:^(NSNotification *notification) {
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] intValue] == AVAudioSessionInterruptionTypeBegan) {
NSUInteger typeValue = [[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] intValue];

if (typeValue == AVAudioSessionInterruptionTypeBegan) {
if (resumeTimer) {
[resumeTimer invalidate];
resumeTimer = nil;
}
FMODBridge_suspendMixer();
} else {
BOOL success = [[AVAudioSession sharedInstance] setActive:TRUE error:nil];
assert(success);
FMODBridge_resumeMixer();

} else if (typeValue == AVAudioSessionInterruptionTypeEnded) {
NSUInteger optionsValue = [[notification.userInfo valueForKey:AVAudioSessionInterruptionOptionKey] intValue];
if (optionsValue & AVAudioSessionInterruptionOptionShouldResume) {
resumeAudioSession(AUDIO_RESUME_RETRY_COUNT);
}
}
}];
}
Expand Down
Binary file modified fmod/lib/ios/libfmodbridge.a
Binary file not shown.
Binary file modified fmod/lib/x86_64-ios/libfmodbridge_simulator.a
Binary file not shown.

0 comments on commit af985c1

Please sign in to comment.