Fixed audio not coming out of the phone speakers while recording on iOS
authorSam Lantinga <slouken@libsdl.org>
Fri, 14 Feb 2020 15:19:34 -0800
changeset 135223cd477fca37e
parent 13521 dfb34b6cbb7a
child 13523 30bc6f3e1ec6
Fixed audio not coming out of the phone speakers while recording on iOS
src/audio/coreaudio/SDL_coreaudio.m
     1.1 --- a/src/audio/coreaudio/SDL_coreaudio.m	Fri Feb 14 14:18:12 2020 -0800
     1.2 +++ b/src/audio/coreaudio/SDL_coreaudio.m	Fri Feb 14 15:19:34 2020 -0800
     1.3 @@ -326,12 +326,18 @@
     1.4      @autoreleasepool {
     1.5          AVAudioSession *session = [AVAudioSession sharedInstance];
     1.6          NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
     1.7 +
     1.8          /* Set category to ambient by default so that other music continues playing. */
     1.9          NSString *category = AVAudioSessionCategoryAmbient;
    1.10 +        NSString *mode = AVAudioSessionModeDefault;
    1.11 +        NSUInteger options = 0;
    1.12          NSError *err = nil;
    1.13  
    1.14          if (open_playback_devices && open_capture_devices) {
    1.15              category = AVAudioSessionCategoryPlayAndRecord;
    1.16 +#if !TARGET_OS_TV
    1.17 +            options = AVAudioSessionCategoryOptionDefaultToSpeaker;
    1.18 +#endif
    1.19          } else if (open_capture_devices) {
    1.20              category = AVAudioSessionCategoryRecord;
    1.21          } else {
    1.22 @@ -348,10 +354,18 @@
    1.23              }
    1.24          }
    1.25  
    1.26 -        if (![session setCategory:category error:&err]) {
    1.27 -            NSString *desc = err.description;
    1.28 -            SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
    1.29 -            return NO;
    1.30 +        if ([session respondsToSelector:@selector(setCategory:mode:options:error:)]) {
    1.31 +            if (![session setCategory:category mode:mode options:options error:&err]) {
    1.32 +                NSString *desc = err.description;
    1.33 +                SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
    1.34 +                return NO;
    1.35 +            }
    1.36 +        } else {
    1.37 +            if (![session setCategory:category error:&err]) {
    1.38 +                NSString *desc = err.description;
    1.39 +                SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
    1.40 +                return NO;
    1.41 +            }
    1.42          }
    1.43  
    1.44          if (open && (open_playback_devices + open_capture_devices) == 1) {