mina
mina

Reputation: 319

Change camera on opentok for ios

I am trying to change the camera of the publisher in OpenTok. In Android it is super easy, but I don't understand how to do it in objective c for ios.

I tried :

if (_publisher.cameraPosition == AVCaptureDevicePositionFront) 
{
    _publisher.cameraPosition = AVCaptureDevicePositionBack; // back camera

} else 
{
    _publisher.cameraPosition = AVCaptureDevicePositionFront; // front camera
}

I have to say that I am a beginner in objective c (and in OpenTok).

How should I do?

Thank you :)

Upvotes: 1

Views: 775

Answers (2)

Ravi Panchal
Ravi Panchal

Reputation: 1355

For Swift 4

      if(publisher.cameraPosition == .back)
        {
            publisher.cameraPosition = .front
        }else{
            publisher.cameraPosition = .back
        }

I haven't checked for Objective C but it will be same like

Upvotes: 1

Tejas Ardeshna
Tejas Ardeshna

Reputation: 4371

Try this one:

func setCameraPosition(_ position: AVCaptureDevicePosition) -> Bool {
        guard let preset = captureSession?.sessionPreset else {
            return false
        }

        let newVideoInput: AVCaptureDeviceInput? = {
            do {
                if position == AVCaptureDevicePosition.back {
                    return try AVCaptureDeviceInput.init(device: backFacingCamera())
                } else if position == AVCaptureDevicePosition.front {
                    return try AVCaptureDeviceInput.init(device: frontFacingCamera())
                } else {
                    return nil
                }
            } catch {
                return nil
            }
        }()

        guard let newInput = newVideoInput else {
            return false
        }

        var success = true

        captureQueue.sync {
            captureSession?.beginConfiguration()
            captureSession?.removeInput(videoInput)

            if captureSession?.canAddInput(newInput) ?? false {
                captureSession?.addInput(newInput)
                videoInput = newInput
            } else {
                success = false
                captureSession?.addInput(videoInput)
            }

            captureSession?.commitConfiguration()
        }

        if success {
            capturePreset = preset
        }

        return success
    }

    func toggleCameraPosition() -> Bool {
        guard hasMultipleCameras else {
            return false
        }

        if  videoInput?.device.position == .front {
            return setCameraPosition(.back)
        } else {
            return setCameraPosition(.front)
        }
    }

Upvotes: 1

Related Questions