How to install camera light on torch in Tokbox?

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

The Tokbox object Publisher

already provides a method swapCamera()

that switches between all the available cameras on the device. But I couldn't find an API to change the flash mode for the currently selected camera.

I tried to get the instance android.hardware.Camera

myself to change it manually, but it didn't work because I got "java.lang.RuntimeException: Connection error to camera service exception. This is because the object Camera

is being used by Tokbox and not released."

I was unable to find access to the instance Camera

that the tokbox is using. It was even deprecated after API level API 21.

Can anyone suggest a way to change camera settings? I have access to View

which video is being viewed on it.

+3


source to share


2 answers


I needed to stop the thread so that the camera app could be started to take a picture. I found a code to release the camera and attach it. Perhaps you can use this code to release the camera, turn on the lights, and reattach the camera.

The following code frees 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;
                }           
            }
        }
    }

      

0


source


torch light will only work with rear camera, so if you post video with front camera then it will depend on video in tokbox.



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")
            }
        }
    }

      

0


source







All Articles