diff --git a/ios/DisplayManager.mm b/ios/DisplayManager.mm index 3e19194bef..36c6115e75 100644 --- a/ios/DisplayManager.mm +++ b/ios/DisplayManager.mm @@ -64,6 +64,9 @@ [self setOriginalFrame: [gameWindow frame]]; [self setOriginalBounds:[gameWindow bounds]]; [self setOriginalTransform:[gameWindow transform]]; + + // TODO: From iOS 13, should use UIScreenDidConnectNotification instead of the below. + // Display connected [[NSNotificationCenter defaultCenter] addObserverForName:UIScreenDidConnectNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull notification) { UIScreen *screen = (UIScreen *) notification.object; @@ -74,8 +77,7 @@ return; } // Ignore mute switch when connected to external display - NSError *error = nil; - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; + iOSCoreAudioSetDisplayConnected(true); [self updateScreen:screen]; }]; // Display disconnected @@ -89,8 +91,7 @@ UIScreen *newScreen = [[self extDisplays] lastObject]; [self updateScreen:newScreen]; } else { - NSError *error = nil; - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error]; + iOSCoreAudioSetDisplayConnected(false); [self updateScreen:[UIScreen mainScreen]]; } }]; diff --git a/ios/iOSCoreAudio.h b/ios/iOSCoreAudio.h index a3c70ccbfc..944e0e81dc 100644 --- a/ios/iOSCoreAudio.h +++ b/ios/iOSCoreAudio.h @@ -19,4 +19,9 @@ // Originally written by jtraynham void iOSCoreAudioInit(); -void iOSCoreAudioShutdown(); \ No newline at end of file +void iOSCoreAudioShutdown(); + +// Ignore mute switch when connected to external display. +// Also, obey other settings. +void iOSCoreAudioUpdateSession(); +void iOSCoreAudioSetDisplayConnected(bool connected); \ No newline at end of file diff --git a/ios/iOSCoreAudio.mm b/ios/iOSCoreAudio.mm index 8129cc9afc..3e1aa0cf29 100644 --- a/ios/iOSCoreAudio.mm +++ b/ios/iOSCoreAudio.mm @@ -28,6 +28,22 @@ #define SAMPLE_RATE 44100 static AudioComponentInstance audioInstance = nil; +static bool g_displayConnected = false; + +void iOSCoreAudioUpdateSession() { + NSError *error = nil; + // Default mode + if (g_displayConnected) { + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; + } else { + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error]; + } +} + +void iOSCoreAudioSetDisplayConnected(bool connected) { + g_displayConnected = connected; + iOSCoreAudioUpdateSession(); +} int NativeMix(short *audio, int numSamples, int sampleRate); @@ -75,7 +91,7 @@ void iOSCoreAudioInit() NSLog(@"%@", error.localizedFailureReason); } } - + if (audioInstance) { // Already running return;