dragermrb / capacitor-plugin-media-capture

Capacitor plugin to capture video
2 stars 2 forks source link

Can't enable flash? #2

Closed gbrits closed 1 year ago

gbrits commented 1 year ago

Hi there, great new package (the utilisation of CameraX) this is an opportunity for the Capacitor community to have a great video package - which is woefully needed / missing from the Ionic ecosystem for some reason?

In my particular use-case I need the flash to be on automatically. Could you steer me in the right direction? I tried adding this code to the bindCaptureUsecase() method inside of the CaptureFragment.kt to no avail.

videoCapture = VideoCapture.withOutput(recorder)

        try {
            cameraProvider.unbindAll()

            var camera = cameraProvider.bindToLifecycle(viewLifecycleOwner,cameraSelector,videoCapture,preview)
            var cameraInfo = camera.getCameraInfo();
            var cameraControl = camera.getCameraControl();

            if (cameraInfo.hasFlashUnit()) {
                Log.d(TAG, "This device DOES have torch (flash) unit.")
                Log.e(TAG, "Camera (& torch) info: ", cameraInfo)
                try {
                    cameraControl.enableTorch(true)
                } catch(exc: CameraControl.OperationCanceledException) {
                    Log.e(TAG, "Turning torch on ERROR: ", exc)
                }
            } else {
                Log.e(TAG, "This device doesn't have torch (flash) unit.")
            }

        } catch (exc: Exception) {
            // we are on main thread, let's reset the controls on the UI.
            Log.e(TAG, "Use case binding failed", exc)
            resetUIandState("bindToLifecycle failed: $exc")
        }

        enableUI(true)
gbrits commented 1 year ago

I'd like to note, the log did show up that my [test] phone had a torch / flash unit. I guess it's just like the underlying Camera API's gripe used to be of the camera already being in use (due to intent usage).

dragermrb commented 1 year ago

Hi @gbrits

I can't implement an option to enable flash at the moment (Android and IOS), I'm pretty busy, but this changes works on my devices.

         try {
             cameraProvider.unbindAll()

+            val camera = cameraProvider.bindToLifecycle(
-            cameraProvider.bindToLifecycle(
                 viewLifecycleOwner,
                 cameraSelector,
                 videoCapture,
                 preview
             )
+
+          if (camera.cameraInfo.hasFlashUnit()) {
+            Log.i(TAG, "Enable torch");
+            camera.cameraControl.enableTorch(true)
+          } else {
+            Log.e(TAG, "No torch available");
+          }
         } catch (exc: Exception) {
             // we are on main thread, let's reset the controls on the UI.
             Log.e(TAG, "Use case binding failed", exc)

Can you share logcat output?

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.