From f4e23553d7e97f56bd16de8fd1a06dfffc9b7d85 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 14 Feb 2020 15:19:34 -0800 Subject: [PATCH] Fixed audio not coming out of the phone speakers while recording on iOS --- src/audio/coreaudio/SDL_coreaudio.m | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m index c20d9b0e6c418..91d8b97606723 100644 --- a/src/audio/coreaudio/SDL_coreaudio.m +++ b/src/audio/coreaudio/SDL_coreaudio.m @@ -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 { @@ -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) {