Skip to content

Commit

Permalink
Fixed audio not coming out of the phone speakers while recording on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 14, 2020
1 parent 922b3dc commit f4e2355
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/audio/coreaudio/SDL_coreaudio.m
Expand Up @@ -326,12 +326,18 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
@autoreleasepool {
AVAudioSession *session = [AVAudioSession sharedInstance];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

/* Set category to ambient by default so that other music continues playing. */
NSString *category = AVAudioSessionCategoryAmbient;
NSString *mode = AVAudioSessionModeDefault;
NSUInteger options = 0;
NSError *err = nil;

if (open_playback_devices && open_capture_devices) {
category = AVAudioSessionCategoryPlayAndRecord;
#if !TARGET_OS_TV
options = AVAudioSessionCategoryOptionDefaultToSpeaker;
#endif
} else if (open_capture_devices) {
category = AVAudioSessionCategoryRecord;
} else {
Expand All @@ -348,10 +354,18 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
}
}

if (![session setCategory:category error:&err]) {
NSString *desc = err.description;
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
return NO;
if ([session respondsToSelector:@selector(setCategory:mode:options:error:)]) {
if (![session setCategory:category mode:mode options:options error:&err]) {
NSString *desc = err.description;
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
return NO;
}
} else {
if (![session setCategory:category error:&err]) {
NSString *desc = err.description;
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
return NO;
}
}

if (open && (open_playback_devices + open_capture_devices) == 1) {
Expand Down

0 comments on commit f4e2355

Please sign in to comment.