Flashlight Camera2 API

user3579059 picture user3579059 · Oct 25, 2015 · Viewed 10.8k times · Source

Can I use camera preview and flashlight at the same time in Android Camera2 API?

When I try use CameraManager.setTorchMode(String cameraId, boolean enabled) it's work fine when camera is not opened. But when Camera is open and I try setTorchMode I receive this exception:

CameraService: setTorchMode: torch mode of camera 0 is not available because camera is in use

Answer

Alkarin picture Alkarin · Jul 13, 2018

The error is indicating that the camera in question, is already in use.

You need to set the FLASH_MODE and CONTROL_AE_MODE accordingly.

mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
// Then Send request to current camera session
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);