Fixed setting the "playandrecord" audio hint on Apple TV The Apple TV doesn't have record capability by default, so activating the audio session with AVAudioSessionCategoryPlayAndRecord fails.
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
diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index 2fdaac7..676072f 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -331,7 +331,7 @@ static void interruption_end(_THIS)
@end
-static BOOL update_audio_session(_THIS, SDL_bool open)
+static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrecord)
{
@autoreleasepool {
AVAudioSession *session = [AVAudioSession sharedInstance];
@@ -356,7 +356,9 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
options &= ~AVAudioSessionCategoryOptionMixWithOthers;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayAndRecord") == 0 ||
SDL_strcasecmp(hint, "playandrecord") == 0) {
- category = AVAudioSessionCategoryPlayAndRecord;
+ if (allow_playandrecord) {
+ category = AVAudioSessionCategoryPlayAndRecord;
+ }
}
} else if (open_playback_devices && open_capture_devices) {
category = AVAudioSessionCategoryPlayAndRecord;
@@ -390,6 +392,11 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
if (open && (open_playback_devices + open_capture_devices) == 1) {
if (![session setActive:YES error:&err]) {
+ if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable &&
+ category == AVAudioSessionCategoryPlayAndRecord) {
+ return update_audio_session(this, open, SDL_FALSE);
+ }
+
NSString *desc = err.description;
SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String);
return NO;
@@ -620,7 +627,7 @@ COREAUDIO_CloseDevice(_THIS)
}
#if !MACOSX_COREAUDIO
- update_audio_session(this, SDL_FALSE);
+ update_audio_session(this, SDL_FALSE, SDL_TRUE);
#endif
/* if callback fires again, feed silence; don't call into the app. */
@@ -945,7 +952,7 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
}
#if !MACOSX_COREAUDIO
- if (!update_audio_session(this, SDL_TRUE)) {
+ if (!update_audio_session(this, SDL_TRUE, SDL_TRUE)) {
return -1;
}