Allow background music to play in the "play and record" case on iOS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index 68aae35..c8cfe10 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -327,44 +327,54 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
AVAudioSession *session = [AVAudioSession sharedInstance];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- /* Set category to ambient by default so that other music continues playing. */
- NSString *category = AVAudioSessionCategoryAmbient;
+ NSString *category = AVAudioSessionCategoryPlayback;
NSString *mode = AVAudioSessionModeDefault;
- NSUInteger options = 0;
+ NSUInteger options = AVAudioSessionCategoryOptionMixWithOthers;
NSError *err = nil;
-
- if (open_playback_devices && open_capture_devices) {
+ const char *hint;
+
+ 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;
-#if !TARGET_OS_TV
- options = AVAudioSessionCategoryOptionDefaultToSpeaker;
-#endif
} else if (open_capture_devices) {
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 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;
+ if (![session.category isEqualToString:category] || session.categoryOptions != options) {
+ 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 (![session.category isEqualToString:category]) {
+ if (![session setCategory:category error:&err]) {
+ NSString *desc = err.description;
+ 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;
}
#else
- /* Calling setPreferredOutputNumberOfChannels seems to break audio output on iOS */
+ /* Calling setPreferredOutputNumberOfChannels seems to break audio output on iOS */
#endif /* TARGET_OS_TV */
}
#endif