Reputation: 184
My agora app has a custom video source as ARView that I transfer using ARVideoKit. How can I implement switching to the front camera?
My initial idea was just to set local video, but it does nothing
@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)
}}
Basically, I need to stop ARSession, probably remove the custom video source and set local video as input.
To set ARView as a video source I followed this tutorial
Upvotes: 0
Views: 1175
Reputation: 2898
You don't need to switch the camera source for Agora, instead you should update the ARKit config to use the front camera
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
}
}
}
Upvotes: 2
Reputation: 1253
instead of enableVideo()/disableVideo() video, try:
self.agoraKit.enableLocalVideo(true/false)
Upvotes: 0