FilterCam
FilterCam copied to clipboard
Change between front and back camera
How can I change between front and back camera?
Hi, @CassianoMouraLeao
You can change devicePosition
property to .front
. Please see the example code https://github.com/nkmrh/FilterCam/blob/master/Example/Example/ViewController.swift#L25
What if I watch to switch between front and back camera on button action, in that case changing the value of devicePosition is not working
@khanskt786 I think I'm experiencing this as well. Although I was playing around with the framework to understand it but for now let's assume the project I downlaoded is in the same state as the OP.
Keep in mind I just began working on the camera frameworks and shaders so my knowledge may not be best practice. Although looking through Apple's Camera Project AVCam, you can tell it's similar to this awesome framework the Author has made for us, and just by looking at the changeCamera(_:) method you can made a crude adaption to the code base.
The first place we should begin is within the the Capture class. Tested on my iPhone X, you'll need to also include conditional code that applies to the target devices for your app. But this should get the camera to switch, although my video isn't mirrored properly which is something you may experience too, but we're only talking about switching cameras here.
func changeCamera(devicePosition: AVCaptureDevice.Position) {
queue.async {
guard let session = self.session else { return }
//remove existing input devices first then add the new devices
session.beginConfiguration()
//https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcam_building_a_camera_app
//https://stackoverflow.com/questions/30755043/avfoundation-toggle-camera-fails-at-canaddinput
for input in session.inputs {
//video
session.removeInput(input as! AVCaptureDeviceInput)
//audio
session.removeInput(input as! AVCaptureDeviceInput)
}
//create new AVCaptureDeviceInput for video
if let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: devicePosition) {
self.videoDevice = videoDevice
guard let videoDeviceInput = self.videoDeviceInput else { return }
if session.canAddInput(videoDeviceInput) {
session.addInput(videoDeviceInput)
} else {
session.addInput(self.videoDeviceInput!)
}
}
//create new AVCaptureDeviceInput for audio
if let audioDevice = AVCaptureDevice.default(.builtInMicrophone, for: .audio, position: .unspecified) {
self.audioDevice = audioDevice
guard let audioDeviceInput = self.audioDeviceInput else { return }
if session.canAddInput(audioDeviceInput) {
session.addInput(audioDeviceInput)
} else {
session.addInput(self.audioDeviceInput!)
}
}
session.commitConfiguration()
}
}
I call this function in the Recorder class. So make another function in the Recorder class to be called in your appropriate ViewController and attach it to your button.
Cheers
Thought I'd quickly add mirroring documentation is provided by the author. Just search for mirroring in the project itself and apply the transform as written to the videoPreviewView.
@khanskt786 I think I'm experiencing this as well. Although I was playing around with the framework to understand it but for now let's assume the project I downlaoded is in the same state as the OP.
Keep in mind I just began working on the camera frameworks and shaders so my knowledge may not be best practice. Although looking through Apple's Camera Project AVCam, you can tell it's similar to this awesome framework the Author has made for us, and just by looking at the changeCamera(_:) method you can made a crude adaption to the code base.
The first place we should begin is within the the Capture class. Tested on my iPhone X, you'll need to also include conditional code that applies to the target devices for your app. But this should get the camera to switch, although my video isn't mirrored properly which is something you may experience too, but we're only talking about switching cameras here.
func changeCamera(devicePosition: AVCaptureDevice.Position) { queue.async { guard let session = self.session else { return } //remove existing input devices first then add the new devices session.beginConfiguration() //https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcam_building_a_camera_app //https://stackoverflow.com/questions/30755043/avfoundation-toggle-camera-fails-at-canaddinput for input in session.inputs { //video session.removeInput(input as! AVCaptureDeviceInput) //audio session.removeInput(input as! AVCaptureDeviceInput) } //create new AVCaptureDeviceInput for video if let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: devicePosition) { self.videoDevice = videoDevice guard let videoDeviceInput = self.videoDeviceInput else { return } if session.canAddInput(videoDeviceInput) { session.addInput(videoDeviceInput) } else { session.addInput(self.videoDeviceInput!) } } //create new AVCaptureDeviceInput for audio if let audioDevice = AVCaptureDevice.default(.builtInMicrophone, for: .audio, position: .unspecified) { self.audioDevice = audioDevice guard let audioDeviceInput = self.audioDeviceInput else { return } if session.canAddInput(audioDeviceInput) { session.addInput(audioDeviceInput) } else { session.addInput(self.audioDeviceInput!) } } session.commitConfiguration() } }
I call this function in the Recorder class. So make another function in the Recorder class to be called in your appropriate ViewController and attach it to your button.
Cheers
thanks for your replay but please where to put this function and when call it ?