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

This commit is contained in:
Sam Lantinga 2020-02-14 15:19:34 -08:00
parent 922b3dc3e7
commit f4e23553d7
1 changed files with 18 additions and 4 deletions

View File

@ -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) {