Search code examples
androidcameraopentokflashlighttokbox

How to set the camera light on torch in Tokbox?


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.


Solution

  • 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;
                    }           
                }
            }
        }