Search code examples
iosswiftvideocall

Toggle camera in OpenTok Swift SDK


I am using OpenTok SDK for Video calling in my application. I need to implement camera Toggle functionality on a button click. But its not working.

I am using

@IBAction func cameraButtonAction(_ sender: Any) { publisher?.cameraPosition = AVCaptureDevice.Position.back } on button click

And publisher is created when session is connected

  func sessionDidConnect(_ session: OTSession) {
    print("The client connected to the OpenTok session.")
    let settings = OTPublisherSettings()
    settings.name = UIDevice.current.name 
    guard let publisher = OTPublisher(delegate: self, settings: settings) else {
        return
    }

    var error: OTError?
    session.publish(publisher, error: &error)
    guard error == nil else {
        print(error!)
        return
    }

    guard let publisherView = publisher.view else {
        return
    }
    let screenBounds = UIScreen.main.bounds
    publisherView.frame = CGRect(x: screenBounds.width - 150 - 20, y: screenBounds.height - 150 - 20, width: 150, height: 150)
    view.addSubview(publisherView)
}

Solution

  • Seems you are not setting the publisher. It's a simple mistake, could happen to anyone.

    guard let publisher = OTPublisher(delegate: self, settings: settings) else {
        return
    }
    
    self.publisher = publisher
    

    Add this line to your code where you are initializing the publisher.