Commit f4e23553d7e97f56bd16de8fd1a06dfffc9b7d85

Sam Lantinga 2020-02-14T15:19:34

Fixed audio not coming out of the phone speakers while recording on iOS

diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index c20d9b0..91d8b97 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) {