Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle permissions for video capture #85

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.0.26](https://github.com/spoonconsulting/cordova-plugin-simple-camera-preview/compare/v2.0.25...v2.0.26) (2024-09-04)
* **Android:** Fix Android permission RECORD_AUDIO on startVideoCapture

## [2.0.25](https://github.com/spoonconsulting/cordova-plugin-simple-camera-preview/compare/v2.0.24...v2.0.25) (2024-08-16)
* **Android:** Add video recording ability for Android

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.25",
"version": "2.0.26",
"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.25" 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.26" 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
54 changes: 28 additions & 26 deletions src/android/CameraPreviewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.video.FileOutputOptions;
import androidx.camera.video.PendingRecording;
import androidx.camera.video.Quality;
import androidx.camera.video.QualitySelector;
import androidx.camera.video.Recorder;
Expand Down Expand Up @@ -278,11 +279,7 @@ public void hasFlash(HasFlashCallback hasFlashCallback) {
hasFlashCallback.onResult(camera.getCameraInfo().hasFlashUnit());
}

public void startVideoCapture(VideoCallback videoCallback) {
if (ActivityCompat.checkSelfPermission(this.getContext(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, 200);
}

public void startVideoCapture(VideoCallback videoCallback, boolean recordWithAudio) {
if (recording != null) {
recording.stop();
recording = null;
Expand All @@ -307,27 +304,32 @@ public void run() {
}
}, 30000);

recording = videoCapture.getOutput()
.prepareRecording(this.getContext().getApplicationContext(), outputOptions)
.withAudioEnabled()
.start(ContextCompat.getMainExecutor(this.getContext()), videoRecordEvent -> {
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
videoCallback.onStart(true, null);
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) videoRecordEvent;
handler.removeCallbacksAndMessages(null);
if (finalizeEvent.hasError()) {
int errorCode = finalizeEvent.getError();
Throwable errorCause = finalizeEvent.getCause();
videoCallback.onError(errorCode + " " + errorCause);
} else {
videoCallback.onStop(false, Uri.fromFile(videoFile).toString());
Uri savedUri = finalizeEvent.getOutputResults().getOutputUri();
}
recording = null;
}
});

PendingRecording pendingRecording = videoCapture.getOutput()
.prepareRecording(this.getContext().getApplicationContext(), outputOptions);
if (recordWithAudio) {
try {
pendingRecording.withAudioEnabled();
} catch (SecurityException e) {
videoCallback.onError(e.getMessage());
}
}
recording = pendingRecording.start(ContextCompat.getMainExecutor(this.getContext()), videoRecordEvent -> {
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
videoCallback.onStart(true, null);
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) videoRecordEvent;
handler.removeCallbacksAndMessages(null);
if (finalizeEvent.hasError()) {
int errorCode = finalizeEvent.getError();
Throwable errorCause = finalizeEvent.getCause();
videoCallback.onError(errorCode + " " + errorCause);
} else {
videoCallback.onStop(false, Uri.fromFile(videoFile).toString());
Uri savedUri = finalizeEvent.getOutputResults().getOutputUri();
}
recording = null;
}
});
}

public void stopVideoCapture() {
Expand Down
31 changes: 28 additions & 3 deletions src/android/SimpleCameraPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class SimpleCameraPreview extends CordovaPlugin {
private static final int DIRECTION_FRONT = 0;
private static final int DIRECTION_BACK = 1;
private static final int REQUEST_CODE_PERMISSIONS = 4582679;
private static final int VIDEO_REQUEST_CODE_PERMISSIONS = 200;
private static final String REQUIRED_PERMISSION = Manifest.permission.CAMERA;

public SimpleCameraPreview() {
Expand Down Expand Up @@ -78,7 +79,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return initVideoCallback(callbackContext);

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

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

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

if (recordWithAudio && !PermissionHelper.hasPermission(this, Manifest.permission.RECORD_AUDIO)) {
String[] permissions = {Manifest.permission.RECORD_AUDIO};
PermissionHelper.requestPermissions(this, VIDEO_REQUEST_CODE_PERMISSIONS, permissions);
callbackContext.success();
return true;
}

if (this.videoCallbackContext != null) {
fragment.startVideoCapture(new VideoCallback() {
public void onStart(Boolean recording, String nativePath) {
Expand Down Expand Up @@ -173,7 +181,7 @@ public void onError(String errMessage) {
pluginResult.setKeepCallback(true);
videoCallbackContext.sendPluginResult(pluginResult);
}
});
}, recordWithAudio);
}
callbackContext.success();
return true;
Expand Down Expand Up @@ -530,6 +538,23 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
enable(this.options, this.enableCallbackContext);
}
}
if (requestCode == VIDEO_REQUEST_CODE_PERMISSIONS && this.videoCallbackContext != null) {
if (grantResults.length < 1) { return; }

boolean permissionsGranted = this.permissionsGranted(grantResults);
JSONObject data = new JSONObject();
try {
data.put("restartVideoCaptureWithAudio", permissionsGranted);
} catch (JSONException e) {
e.printStackTrace();
videoCallbackContext.error("Cannot start video");
return;
}

PluginResult result = new PluginResult(PluginResult.Status.OK, data);
result.setKeepCallback(true);
this.videoCallbackContext.sendPluginResult(result);
}
}


Expand Down
7 changes: 5 additions & 2 deletions www/SimpleCameraPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var SimpleCameraPreview = function () {};
SimpleCameraPreview.videoInitialized = false;
SimpleCameraPreview.videoCallback = null;

SimpleCameraPreview.startVideoCapture = function (onSuccess, onError) {
SimpleCameraPreview.startVideoCapture = function (options, onSuccess, onError) {
if (!SimpleCameraPreview.videoCallback) {
console.error("Call initVideoCallback first");
onError("Call initVideoCallback first");
Expand All @@ -17,7 +17,10 @@ SimpleCameraPreview.startVideoCapture = function (onSuccess, onError) {
onError("videoCallback not initialized");
return;
}
exec(onSuccess, onError, PLUGIN_NAME, "startVideoCapture");

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

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