Skip to content

Commit

Permalink
separate start and stop video capture
Browse files Browse the repository at this point in the history
  • Loading branch information
YushraJewon committed Jun 10, 2024
1 parent 041a975 commit fd3f009
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 58 deletions.
52 changes: 29 additions & 23 deletions src/android/CameraPreviewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ interface CameraCallback {
void onCompleted(Exception err, String nativePath);
}

interface VideoCallback {
void onStart(Boolean recording, String nativePath);
void onStop(Boolean recording, String nativePath);
interface StartVideoCallback {
void onStartVideo(Exception err, Boolean recording);
}

interface StopVideoCallback {
void onStopVideo(Exception err, String nativePath);
}

interface CameraStartedCallback {
Expand Down Expand Up @@ -107,6 +110,9 @@ public class CameraPreviewFragment extends Fragment {
private static float ratio = (4 / (float) 3);
private static final String TAG = "SimpleCameraPreview";
private String captureDevice;
private String filename;
private File videoFile;
private VideoRecordEvent videoRecordEvent;

public CameraPreviewFragment() {

Expand Down Expand Up @@ -277,19 +283,18 @@ public void hasFlash(HasFlashCallback hasFlashCallback) {
hasFlashCallback.onResult(camera.getCameraInfo().hasFlashUnit());
}

public void captureVideo(VideoCallback videoCallback) {
public void startCaptureVideo(StartVideoCallback startVideoCallback) {
if (recording != null) {
recording.stop();
recording = null;
return;
}
UUID uuid = UUID.randomUUID();

String filename = uuid.toString() + ".mp4";
this.filename = uuid.toString() + ".mp4";
if (ActivityCompat.checkSelfPermission(this.getContext(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, 200);
}
File videoFile = new File(
this.videoFile = new File(
getContext().getFilesDir(),
filename
);
Expand All @@ -300,28 +305,29 @@ public void captureVideo(VideoCallback videoCallback) {
.prepareRecording(this.getContext().getApplicationContext(), outputOptions)
.withAudioEnabled()
.start(ContextCompat.getMainExecutor(this.getContext()), videoRecordEvent -> {
this.videoRecordEvent = videoRecordEvent;
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
videoCallback.onStart(true, null);
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) videoRecordEvent;
if (finalizeEvent.hasError()) {
// Handle the error
int errorCode = finalizeEvent.getError();
Throwable errorCause = finalizeEvent.getCause();
Log.e(TAG, "Video recording error: " + errorCode, errorCause);
} else {
// Handle video saved
videoCallback.onStop(false, Uri.fromFile(videoFile).toString());
Uri savedUri = finalizeEvent.getOutputResults().getOutputUri();
Log.i(TAG, "Video saved to: " + savedUri);
}
recording = null;
startVideoCallback.onStartVideo(null, true);
}
// Other event types can be handled if needed
});

}

public void stopCaptureVideo(StopVideoCallback stopVideoCallback) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) this.videoRecordEvent;
if (finalizeEvent.hasError()) {
// Handle the error
int errorCode = finalizeEvent.getError();
Throwable errorCause = finalizeEvent.getCause();
Log.e(TAG, "Video recording error: " + errorCode, errorCause);
} else {
// Handle video saved
stopVideoCallback.onStopVideo(null, Uri.fromFile(videoFile).toString());
Uri savedUri = finalizeEvent.getOutputResults().getOutputUri();
Log.i(TAG, "Video saved to: " + savedUri);
}
recording = null;
}


public void takePicture(boolean useFlash, CameraCallback takePictureCallback) {
Expand Down
57 changes: 24 additions & 33 deletions src/android/SimpleCameraPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
case "torchSwitch":
return torchSwitch(args.getBoolean(0), callbackContext);

case "captureVideo":
return captureVideo(callbackContext);

case "startCaptureVideo":
return startVideoCapture(callbackContext);
case "stopCaptureVideo":
return stopVideoCapture(callbackContext);
case "deviceHasFlash":
return deviceHasFlash(callbackContext);

Expand Down Expand Up @@ -270,44 +271,34 @@ public void fetchLocation() {
}
}

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

fragment.captureVideo(new VideoCallback() {
public void onStart(Boolean recording, String nativePath) {
JSONObject data = new JSONObject();
if (recording) {
try {
data.put("recording", true);
data.put("nativePath", null);
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Cannot send recording data");
return;
}

PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, data);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}
fragment.startCaptureVideo((Exception err, Boolean recording) -> {
if (err == null) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, recording);
callbackContext.sendPluginResult(pluginResult);
} else {
callbackContext.error(err.getMessage());
}
});
return true;
}

public void onStop(Boolean recording, String nativePath) {
JSONObject data = new JSONObject();
try {
data.put("recording", false);
data.put("nativePath", nativePath);
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Cannot send recording data");
return;
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, data);
pluginResult.setKeepCallback(true);
private boolean stopVideoCapture(CallbackContext callbackContext) {
if (fragment == null) {
callbackContext.error("Camera is closed");
return true;
}
fragment.stopCaptureVideo((Exception err, String nativePath) -> {
if (err == null) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, nativePath);
callbackContext.sendPluginResult(pluginResult);
} else {
callbackContext.error(err.getMessage());
}
});
return true;
Expand Down
8 changes: 6 additions & 2 deletions www/SimpleCameraPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ SimpleCameraPreview.capture = function (options, onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "capture", [options.flash]);
};

SimpleCameraPreview.captureVideo = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "captureVideo");
SimpleCameraPreview.startCaptureVideo = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "startCaptureVideo");
};

SimpleCameraPreview.stopCaptureVideo = function (onSuccess, onError) {
exec(onSuccess, onError, PLUGIN_NAME, "stopCaptureVideo");
};

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

0 comments on commit fd3f009

Please sign in to comment.