From d7712fabfa274a473b36d911eec6f3e3643f2c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20A=2E=20A=CC=81lvarez?= Date: Sun, 31 Mar 2024 16:50:26 +0200 Subject: [PATCH] more watchOS shenanigans --- .../InterfaceController.m | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/Mini vMac WatchKit Extension/InterfaceController.m b/Mini vMac WatchKit Extension/InterfaceController.m index 27cdd67..60c77ad 100644 --- a/Mini vMac WatchKit Extension/InterfaceController.m +++ b/Mini vMac WatchKit Extension/InterfaceController.m @@ -33,6 +33,7 @@ static NSObject *sharedEmulator = nil; { WKExtendedRuntimeSession *runtimeSession; BOOL hasStartedEmulator; + AVAudioEngine *audioEngine; } + (void)load { @@ -114,8 +115,7 @@ static NSObject *sharedEmulator = nil; - (void)didDeactivate { // This method is called when watch view controller is no longer visible [super didDeactivate]; - [runtimeSession invalidate]; - sharedEmulator.running = NO; + //sharedEmulator.running = NO; } - (void)sessionReachabilityDidChange:(WCSession *)session { @@ -123,20 +123,27 @@ static NSObject *sharedEmulator = nil; } - (void)startRuntimeSession { - runtimeSession = [WKExtendedRuntimeSession new]; - runtimeSession.delegate = self; - [runtimeSession start]; + if (runtimeSession == nil || runtimeSession.state == WKExtendedRuntimeSessionStateInvalid) { + runtimeSession = [WKExtendedRuntimeSession new]; + runtimeSession.delegate = self; + } + if (runtimeSession.state == WKExtendedRuntimeSessionStateNotStarted) { + [runtimeSession start]; + } } - (void)startOrResumeEmulator { if (!hasStartedEmulator) { hasStartedEmulator = YES; [sharedEmulator performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO]; + [self startAudioEngine]; } else { sharedEmulator.running = YES; } } +#define LTOVRTCP_SERVER "37.187.76.115:1984" + - (void)loadAndStartEmulator { #ifdef LTOVRTCP_SERVER setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1); @@ -208,12 +215,13 @@ static NSObject *sharedEmulator = nil; - (void)extendedRuntimeSessionDidStart:(WKExtendedRuntimeSession *)extendedRuntimeSession { #if TARGET_OS_SIMULATOR == 0 - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback - mode:AVAudioSessionModeDefault - routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongFormAudio - options:0 - error:NULL]; - [[AVAudioSession sharedInstance] activateWithOptions:0 completionHandler:^(BOOL activated, NSError * _Nullable error) { + AVAudioSession *audioSession = [AVAudioSession sharedInstance]; + [audioSession setCategory:AVAudioSessionCategoryPlayback + mode:AVAudioSessionModeDefault + routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongFormAudio + options:0 + error:NULL]; + [audioSession activateWithOptions:0 completionHandler:^(BOOL activated, NSError * _Nullable error) { // network only works on watchOS when there's an active audio session [self startOrResumeEmulator]; }]; @@ -223,11 +231,27 @@ static NSObject *sharedEmulator = nil; } - (void)extendedRuntimeSession:(WKExtendedRuntimeSession *)extendedRuntimeSession didInvalidateWithReason:(WKExtendedRuntimeSessionInvalidationReason)reason error:(NSError *)error { - NSLog(@"Runtime session invalidated: %@", error); + NSLog(@"Runtime session inv alidated (%d): %@", (int)reason, error); + sharedEmulator.showAlert(@"Runtime session invalidated", [NSString stringWithFormat:@"reason=%d, error=%@", (int)reason, error]); } - (void)extendedRuntimeSessionWillExpire:(WKExtendedRuntimeSession *)extendedRuntimeSession { NSLog(@"Extended runtime session will expire"); } +- (void)startAudioEngine { + NSError *error = nil; + audioEngine = [AVAudioEngine new]; + AVAudioFormat *audioFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:22255 channels:1]; + [audioEngine enableManualRenderingMode:AVAudioEngineManualRenderingModeRealtime + format:audioFormat + maximumFrameCount:3 // DesiredMinFilledSoundBuffs + error:&error]; + if (error == nil) { + [audioEngine startAndReturnError:&error]; + } + if (error != nil) { + NSLog(@"Error starting audioEngine: %@", error); + } +} @end