Allow background music to play in the "play and record" case on iOS

This commit is contained in:
Sam Lantinga 2020-03-26 19:30:17 -07:00
parent 369e5f33fb
commit abdc5cbf24

View File

@ -327,44 +327,54 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
AVAudioSession *session = [AVAudioSession sharedInstance]; AVAudioSession *session = [AVAudioSession sharedInstance];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
/* Set category to ambient by default so that other music continues playing. */ NSString *category = AVAudioSessionCategoryPlayback;
NSString *category = AVAudioSessionCategoryAmbient;
NSString *mode = AVAudioSessionModeDefault; NSString *mode = AVAudioSessionModeDefault;
NSUInteger options = 0; NSUInteger options = AVAudioSessionCategoryOptionMixWithOthers;
NSError *err = nil; NSError *err = nil;
const char *hint;
if (open_playback_devices && open_capture_devices) { hint = SDL_GetHint(SDL_HINT_AUDIO_CATEGORY);
if (hint) {
if (SDL_strcasecmp(hint, "AVAudioSessionCategoryAmbient") == 0) {
category = AVAudioSessionCategoryAmbient;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategorySoloAmbient") == 0) {
category = AVAudioSessionCategorySoloAmbient;
options &= ~AVAudioSessionCategoryOptionMixWithOthers;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayback") == 0 ||
SDL_strcasecmp(hint, "playback") == 0) {
category = AVAudioSessionCategoryPlayback;
options &= ~AVAudioSessionCategoryOptionMixWithOthers;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayAndRecord") == 0 ||
SDL_strcasecmp(hint, "playandrecord") == 0) {
category = AVAudioSessionCategoryPlayAndRecord;
}
} else if (open_playback_devices && open_capture_devices) {
category = AVAudioSessionCategoryPlayAndRecord; category = AVAudioSessionCategoryPlayAndRecord;
#if !TARGET_OS_TV
options = AVAudioSessionCategoryOptionDefaultToSpeaker;
#endif
} else if (open_capture_devices) { } else if (open_capture_devices) {
category = AVAudioSessionCategoryRecord; category = AVAudioSessionCategoryRecord;
} else {
const char *hint = SDL_GetHint(SDL_HINT_AUDIO_CATEGORY);
if (hint) {
if (SDL_strcasecmp(hint, "AVAudioSessionCategoryAmbient") == 0) {
category = AVAudioSessionCategoryAmbient;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategorySoloAmbient") == 0) {
category = AVAudioSessionCategorySoloAmbient;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayback") == 0 ||
SDL_strcasecmp(hint, "playback") == 0) {
category = AVAudioSessionCategoryPlayback;
}
}
} }
#if !TARGET_OS_TV
if (category == AVAudioSessionCategoryPlayAndRecord) {
options |= AVAudioSessionCategoryOptionDefaultToSpeaker;
}
#endif
if ([session respondsToSelector:@selector(setCategory:mode:options:error:)]) { if ([session respondsToSelector:@selector(setCategory:mode:options:error:)]) {
if (![session setCategory:category mode:mode options:options error:&err]) { if (![session.category isEqualToString:category] || session.categoryOptions != options) {
NSString *desc = err.description; if (![session setCategory:category mode:mode options:options error:&err]) {
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String); NSString *desc = err.description;
return NO; SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
return NO;
}
} }
} else { } else {
if (![session setCategory:category error:&err]) { if (![session.category isEqualToString:category]) {
NSString *desc = err.description; if (![session setCategory:category error:&err]) {
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String); NSString *desc = err.description;
return NO; SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
return NO;
}
} }
} }
@ -864,7 +874,7 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
this->spec.channels = session.preferredOutputNumberOfChannels; this->spec.channels = session.preferredOutputNumberOfChannels;
} }
#else #else
/* Calling setPreferredOutputNumberOfChannels seems to break audio output on iOS */ /* Calling setPreferredOutputNumberOfChannels seems to break audio output on iOS */
#endif /* TARGET_OS_TV */ #endif /* TARGET_OS_TV */
} }
#endif #endif