Giter Club home page Giter Club logo

Comments (5)

nkmrh avatar nkmrh commented on May 25, 2024

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

from filtercam.

khanskt786 avatar khanskt786 commented on May 25, 2024

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

from filtercam.

markhmwong avatar markhmwong commented on May 25, 2024

@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

from filtercam.

markhmwong avatar markhmwong commented on May 25, 2024

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.

from filtercam.

amro153 avatar amro153 commented on May 25, 2024

@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 ?

from filtercam.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.