切换视频源 Agora.io

时间:2021-03-30 03:03:14

标签: arkit agora.io sceneview

我的 agora 应用有一个自定义视频源作为 ARView,我使用 ARVideoKit 传输。如何实现切换到前置摄像头?

我最初的想法只是设置本地视频,但没有任何作用

@objc private func switchCamera() {
    captureType = captureType == .ar ? .camera : .ar
    setCaptureType(to: captureType)
}

private func stopScene(){
    arRecorder.rest()
    sceneView.session.pause()
}

private func startScene() {        
    sceneView.session.run(configuration)
    arRecorder.prepare(configuration)
}

private func setCaptureType(to type: CaptureType) {
switch type {
case .ar:
    startScene()
    agoraKit.disableVideo()
    agoraKit.setVideoSource(arVideoSource)
    
case .camera:
    stopScene()
    
    agoraKit.enableVideo()
    agoraKit.setVideoSource(nil)
    let videoCanvas = AgoraRtcVideoCanvas()
    videoCanvas.uid = 0
    videoCanvas.renderMode = .hidden
    videoCanvas.view = localVideoView
    agoraKit.setupLocalVideo(videoCanvas)
}}

基本上,我需要停止 ARSession,可能删除自定义视频源并将本地视频设置为输入。

为了将 ARView 设置为视频源,我关注了 this tutorial

2 个答案:

答案 0 :(得分:2)

您无需为 Agora 切换摄像头来源,而是应更新 ARKit 配置以使用前置摄像头

class ViewController: UIViewController, ARSCNViewDelegate, RenderARDelegate, RecordARDelegate {
  weak var cameraFlipBtn : UIButton!
  enum cameraFacing {
      case front
      case back
  }
  var activeCam: cameraFacing = .back
  override func viewDidLoad() {
    super.viewDidLoad()
    // Configure ARKit Session
    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = [.horizontal, .vertical]
    self.activeCam = .back // set the active camera
    // Reverse camera button
    if ARFaceTrackingConfiguration.isSupported {
      // add reverse camera button
      let reverseCameraBtn = UIButton()
      reverseCameraBtn.frame = CGRect(x: self.view.bounds.maxX-75, y: 25, width: 50, height: 50)
      if let imageReverseCameraBtn = UIImage(named: "cameraFlip") {
          reverseCameraBtn.setImage(imageReverseCameraBtn, for: .normal)
      }
      self.view.insertSubview(reverseCameraBtn, at: 3)
      self.cameraFlipBtn = reverseCameraBtn
    }
    self.cameraFlipBtn.addTarget(self, action: #selector(switchCamera), for: .touchDown)
  }
  @objc private func switchCamera() {
    if self.activeCam == .back {
      // switch to front config
      let configuration = ARFaceTrackingConfiguration()
      configuration.isLightEstimationEnabled = true
      // run the config to swap the camera
      self.sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
      self.activeCam = .front
    } else {
      // switch to back cam config
      let configuration = ARWorldTrackingConfiguration()
      configuration.planeDetection = [.horizontal, .vertical]
      // run the config to swap the camera
      self.sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
      self.activeCam = .back
    }
  }
}

答案 1 :(得分:0)

代替 enableVideo()/disableVideo() 视频,尝试:

self.agoraKit.enableLocalVideo(true/false)
相关问题