0

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

我最初的想法只是设置本地视频,但它什么也没做

@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 设置为视频源,我遵循了本教程

4

2 回答 2

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
    }
  }
}
于 2021-04-07T20:18:40.087 回答
0

而不是 enableVideo()/disableVideo() 视频,尝试:

self.agoraKit.enableLocalVideo(true/false)
于 2021-04-01T10:06:21.117 回答