From 8945ad209b4c6dbadea7eff1e4db2cccb6c4c7d0 Mon Sep 17 00:00:00 2001 From: YushraJewon Date: Mon, 21 Oct 2024 14:19:46 +0400 Subject: [PATCH] add timer for video recording ios --- src/ios/SimpleCameraPreview.m | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/ios/SimpleCameraPreview.m b/src/ios/SimpleCameraPreview.m index 5fa2b28..3dcde0a 100644 --- a/src/ios/SimpleCameraPreview.m +++ b/src/ios/SimpleCameraPreview.m @@ -367,26 +367,37 @@ - (void) initVideoCallback:(CDVInvokedUrlCommand*)command { [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } +NSTimer *captureTimer; - (void)startVideoCapture:(CDVInvokedUrlCommand*)command { - if (self.sessionManager != nil && !self.sessionManager.movieFileOutput.isRecording) { + if (self.sessionManager == nil || self.sessionManager.movieFileOutput.isRecording) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Session not initialized or already recording"]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } else { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"NoCloud"]; NSString* uniqueFileName = [NSString stringWithFormat:@"%@.mp4",[[NSUUID UUID] UUIDString]]; NSString *dataPath = [libraryDirectory stringByAppendingPathComponent:uniqueFileName]; NSURL *fileURL = [NSURL fileURLWithPath:dataPath]; + [self.sessionManager startRecording:fileURL recordingDelegate:self]; - } else { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Session not initialized or already recording"]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + captureTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 + target:self + selector:@selector(stopVideoCapture:) + userInfo:nil + repeats:NO]; } } - (void)stopVideoCapture:(CDVInvokedUrlCommand*)command { - if (self.sessionManager != nil && self.sessionManager.movieFileOutput.isRecording) { - [self.sessionManager stopRecording]; + if (self.sessionManager == nil || !self.sessionManager.movieFileOutput.isRecording) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Session not initialized or not recording"]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } else { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Session not initialized or not recording"]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self.sessionManager stopRecording]; + if (captureTimer != nil) { + [captureTimer invalidate]; + captureTimer = nil; + } } }