Skip to content

Commit

Permalink
Merge pull request #90 from spoonconsulting/video-limit-config
Browse files Browse the repository at this point in the history
Video limit param
  • Loading branch information
dinitri authored Nov 28, 2024
2 parents 0e55296 + c5eac4f commit 942a44a
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.0.32](https://github.com/spoonconsulting/cordova-plugin-simple-camera-preview/compare/2.0.31...2.0.32) (2024-11-28)
* **Android:** Use videoDuration parameter to configure duration of a video
* **iOS:** Use videoDuration parameter to configure duration of a video

## [2.0.31](https://github.com/spoonconsulting/cordova-plugin-simple-camera-preview/compare/2.0.30...2.0.31) (2024-11-04)
* **iOS:** Automatic stop video after 30s

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spoonconsulting/cordova-plugin-simple-camera-preview",
"version": "2.0.31",
"version": "2.0.32",
"description": "Cordova plugin that allows camera interaction from HTML code for showing camera preview below or on top of the HTML.",
"keywords": [
"cordova",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<plugin id="@spoonconsulting/cordova-plugin-simple-camera-preview" version="2.0.31" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="@spoonconsulting/cordova-plugin-simple-camera-preview" version="2.0.32" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">

<name>cordova-plugin-simple-camera-preview</name>
<description>Cordova plugin that allows camera interaction from HTML code. Show camera preview popup on top of the HTML.</description>
Expand Down
4 changes: 2 additions & 2 deletions src/android/CameraPreviewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void hasFlash(HasFlashCallback hasFlashCallback) {
hasFlashCallback.onResult(camera.getCameraInfo().hasFlashUnit());
}

public void startVideoCapture(VideoCallback videoCallback, boolean recordWithAudio) {
public void startVideoCapture(VideoCallback videoCallback, boolean recordWithAudio, int videoDuration) {
if (recording != null) {
recording.stop();
recording = null;
Expand All @@ -308,7 +308,7 @@ public void startVideoCapture(VideoCallback videoCallback, boolean recordWithAud
public void run() {
stopVideoCapture();
}
}, 30000);
}, videoDuration);

PendingRecording pendingRecording = videoCapture.getOutput()
.prepareRecording(this.getContext().getApplicationContext(), outputOptions);
Expand Down
22 changes: 19 additions & 3 deletions src/android/SimpleCameraPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return initVideoCallback(callbackContext);

case "startVideoCapture":
return startVideoCapture(args.getBoolean(0), callbackContext);
return startVideoCapture((JSONObject) args.get(0), callbackContext);

case "stopVideoCapture":
return stopVideoCapture(callbackContext);
Expand Down Expand Up @@ -120,12 +120,28 @@ private boolean initVideoCallback(CallbackContext callbackContext) {
return true;
}

private boolean startVideoCapture(boolean recordWithAudio, CallbackContext callbackContext) {
private boolean startVideoCapture(JSONObject options, CallbackContext callbackContext) {
if (fragment == null) {
callbackContext.error("Camera is closed");
return true;
}

boolean recordWithAudio;
try {
recordWithAudio = options.getBoolean("recordWithAudio");
} catch (JSONException e) {
e.printStackTrace();
recordWithAudio = false;
}

int videoDuration;
try {
videoDuration = options.getInt("videoDurationMs");
} catch (JSONException e) {
e.printStackTrace();
videoDuration = 3000;
}

if (recordWithAudio && !PermissionHelper.hasPermission(this, Manifest.permission.RECORD_AUDIO)) {
String[] permissions = {Manifest.permission.RECORD_AUDIO};
PermissionHelper.requestPermissions(this, VIDEO_REQUEST_CODE_PERMISSIONS, permissions);
Expand Down Expand Up @@ -181,7 +197,7 @@ public void onError(String errMessage) {
pluginResult.setKeepCallback(true);
videoCallbackContext.sendPluginResult(pluginResult);
}
}, recordWithAudio);
}, recordWithAudio, videoDuration);
}
callbackContext.success();
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/ios/CameraSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- (BOOL) deviceHasUltraWideCamera;
- (void) deallocSession;
- (void) updateOrientation:(AVCaptureVideoOrientation)orientation;
- (void) startRecording:(NSURL *)fileURL recordingDelegate:(id<AVCaptureFileOutputRecordingDelegate>)recordingDelegate;
- (void) startRecording:(NSURL *)fileURL recordingDelegate:(id<AVCaptureFileOutputRecordingDelegate>)recordingDelegate videoDurationMs:(NSInteger)videoDuration;
- (void) stopRecording;
- (AVCaptureVideoOrientation) getCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
+ (AVCaptureSessionPreset) calculateResolution:(NSInteger)targetSize;
Expand Down
6 changes: 4 additions & 2 deletions src/ios/CameraSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,16 @@ - (void)switchCameraTo:(NSString*)cameraMode completion:(void (^)(BOOL success))
});
}

- (void)startRecording:(NSURL *)fileURL recordingDelegate:(id<AVCaptureFileOutputRecordingDelegate>)recordingDelegate {
- (void)startRecording:(NSURL *)fileURL recordingDelegate:(id<AVCaptureFileOutputRecordingDelegate>)recordingDelegate videoDurationMs:(NSInteger)videoDurationMs {
if (!self.movieFileOutput.isRecording) {
AVCaptureConnection *connection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
if ([connection isVideoOrientationSupported]) {
connection.videoOrientation = [self getCurrentOrientation];
}
[self.movieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:recordingDelegate];
_videoTimer = [NSTimer scheduledTimerWithTimeInterval:30.0

NSInteger videoDurationInSec = videoDurationMs / 1000;
_videoTimer = [NSTimer scheduledTimerWithTimeInterval:videoDurationInSec
target:self
selector:@selector(stopRecording)
userInfo:nil
Expand Down
5 changes: 4 additions & 1 deletion src/ios/SimpleCameraPreview.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,16 @@ - (void) initVideoCallback:(CDVInvokedUrlCommand*)command {
}

- (void)startVideoCapture:(CDVInvokedUrlCommand*)command {
NSDictionary* config = command.arguments[0];
NSInteger videoDuration = ((NSNumber*)config[@"videoDurationMs"]).intValue;

if (self.sessionManager != nil && !self.sessionManager.movieFileOutput.isRecording) {
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];
[self.sessionManager startRecording:fileURL recordingDelegate:self videoDurationMs:videoDuration];
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Session not initialized or already recording"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down
3 changes: 2 additions & 1 deletion www/SimpleCameraPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ SimpleCameraPreview.startVideoCapture = function (options, onSuccess, onError) {

options = options || {};
options.recordWithAudio = options.recordWithAudio != null ? options.recordWithAudio : true;
exec(onSuccess, onError, PLUGIN_NAME, "startVideoCapture", [options.recordWithAudio]);
options.videoDurationMs = options.videoDurationMs != null ? options.videoDurationMs : 3000;
exec(onSuccess, onError, PLUGIN_NAME, "startVideoCapture", [options]);
};

SimpleCameraPreview.stopVideoCapture = function (onSuccess, onError) {
Expand Down

0 comments on commit 942a44a

Please sign in to comment.