How to set the camera light on torch in Tokbox?

1.4k views Asked by At

I am using TokBox for an android project. I need to add a button which would turn the flash light on torch mode.

Tokbox Publisher object already provides a swapCamera() method which switches between all the available cameras of the device. But I couldn't find any API to change the flash light mode for the currently selected camera.

I tried getting an instance of android.hardware.Camera myself to change it manually, but it didn't work because I got the "java.lang.RuntimeException: Fail to connect to camera service" exception. It is because the Camera object is being used by Tokbox and is not released.

I could find no way to access the Camera instance that Tokbox is using either. It is even deprecated since android API level 21.

Can anyone suggest a way to change the camera parameters? I have access to the View that the video is being previewed on it.

2

There are 2 answers

2
Henrik Lund On BEST ANSWER

I needed to stop the stream to be able to start the camera app to take a picture. I've found code to release the camera and attach it. Maybe you can use this code to release the camera, turn on the light and then attach the camera again

The following code releases the camera:

public void ReleaseCamera()
    {
        if (_myPub != null) {
            _myPub.PublishVideo = false;

            BaseVideoCapturer bvc = _myPub.Capturer;
            if (bvc != null) {
                bvc.StopCapture ();
                bvc.Destroy ();
            }
        }
    }

And this code attaches the camera again:

public void AttachCamera()
    {
        if (_myPub != null) {
            BaseVideoCapturer bvc = _myPub.Capturer;
            if (bvc != null) {
                if (bvc.IsCaptureStarted == false) {
                    bvc.Init ();
                    bvc.StartCapture ();
                    _myPub.PublishVideo = true;
                }           
            }
        }
    }
1
Waseem Sarwar On

torch light will work only with back camera, so if you are publishing video with front camera then it will freez tokbox video.

if(publisher.cameraPosition == .back){

        if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), device.hasTorch {
            do {
                try device.lockForConfiguration()
                let torchOn = !device.isTorchActive
                try device.setTorchModeOnWithLevel(1.0)
                device.torchMode = torchOn ? .on : .off
                device.unlockForConfiguration()
            } catch {
                print("error")
            }
        }
    }