diff --git a/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate b/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate index f0bcd21..1638a5e 100644 Binary files a/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate and b/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/camera/CameraManager.swift b/camera/CameraManager.swift index 37107c8..a33c521 100644 --- a/camera/CameraManager.swift +++ b/camera/CameraManager.swift @@ -160,53 +160,7 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { } set(newCameraOutputMode) { if newCameraOutputMode != self.currentCameraOutputMode { - self.captureSession?.beginConfiguration() - - // remove current setting - switch self.currentCameraOutputMode { - case .StillImage: - if let validStillImageOutput = self.stillImageOutput? { - self.captureSession?.removeOutput(validStillImageOutput) - } - case .VideoOnly, .VideoWithMic: - if let validMovieOutput = self.movieOutput? { - self.captureSession?.removeOutput(validMovieOutput) - } - if self.currentCameraOutputMode == .VideoWithMic { - if let validMic = self.mic? { - self.captureSession?.removeInput(validMic) - } - } - } - // configure new devices - switch newCameraOutputMode { - case .StillImage: - if (self.stillImageOutput == nil) { - self._setupOutputs() - } - if let validStillImageOutput = self.stillImageOutput? { - self.captureSession?.addOutput(validStillImageOutput) - } - case .VideoOnly, .VideoWithMic: - if (self.movieOutput == nil) { - self._setupOutputs() - } - if let validMovieOutput = self.movieOutput? { - self.captureSession?.addOutput(validMovieOutput) - } - if newCameraOutputMode == .VideoWithMic { - if (self.mic == nil) { - self._setupMic() - } - if let validMic = self.mic? { - self.captureSession?.addInput(validMic) - } - } - } - self.captureSession?.commitConfiguration() - self._orientationChanged() - - self.currentCameraOutputMode = newCameraOutputMode + self._setupOutputMode(newCameraOutputMode) } } } @@ -379,7 +333,7 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { func startRecordingVideo() { if self.cameraOutputMode != .StillImage { - self.movieOutput?.startRecordingToOutputFileURL(self.tempFilePath, recordingDelegate: self) + self._getMovieOutput().startRecordingToOutputFileURL(self.tempFilePath, recordingDelegate: self) } else { self._show("Capture session output still image", message: "I can only take pictures") } @@ -439,6 +393,38 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { } } + private func _getMovieOutput() -> AVCaptureMovieFileOutput + { + var shouldReinitializeMovieOutput = self.movieOutput == nil + if !shouldReinitializeMovieOutput { + if let connection = self.movieOutput!.connectionWithMediaType(AVMediaTypeVideo) { + shouldReinitializeMovieOutput = shouldReinitializeMovieOutput || !connection.active + } + } + + if shouldReinitializeMovieOutput { + self.movieOutput = AVCaptureMovieFileOutput() + self.captureSession?.addOutput(self.movieOutput) + } + return self.movieOutput! + } + + private func _getCaptureSession() -> AVCaptureSession + { + var shouldReinitializeCaptureSession = self.captureSession == nil + if !shouldReinitializeCaptureSession { + shouldReinitializeCaptureSession = shouldReinitializeCaptureSession || !self.captureSession!.running + shouldReinitializeCaptureSession = shouldReinitializeCaptureSession || self.captureSession!.interrupted + } + + if shouldReinitializeCaptureSession { + self.captureSession = AVCaptureSession() + self._setupOutputs() + self._setupPreviewLayer() + } + return self.captureSession! + } + @objc private func _orientationChanged() { var currentConnection: AVCaptureConnection?; @@ -446,7 +432,7 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { case .StillImage: currentConnection = self.stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo) case .VideoOnly, .VideoWithMic: - currentConnection = self.movieOutput?.connectionWithMediaType(AVMediaTypeVideo) + currentConnection = self._getMovieOutput().connectionWithMediaType(AVMediaTypeVideo) } if let validPreviewLayer = self.previewLayer? { @@ -491,7 +477,7 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { validCaptureSession.sessionPreset = AVCaptureSessionPresetHigh self._addVideoInput() self._setupOutputs() - self.cameraOutputMode = self.currentCameraOutputMode + self._setupOutputMode(self.currentCameraOutputMode) self.cameraOutputQuality = self.currentCameraOutputQuality self._setupPreviewLayer() validCaptureSession.commitConfiguration() @@ -582,7 +568,56 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { } } } - + + private func _setupOutputMode(newCameraOutputMode: CameraOutputMode) + { + self.captureSession?.beginConfiguration() + + if (self.currentCameraOutputMode != newCameraOutputMode) { + // remove current setting + switch self.currentCameraOutputMode { + case .StillImage: + if let validStillImageOutput = self.stillImageOutput? { + self.captureSession?.removeOutput(validStillImageOutput) + } + case .VideoOnly, .VideoWithMic: + if let validMovieOutput = self.movieOutput? { + self.captureSession?.removeOutput(validMovieOutput) + } + if self.currentCameraOutputMode == .VideoWithMic { + if let validMic = self.mic? { + self.captureSession?.removeInput(validMic) + } + } + } + } + + // configure new devices + switch newCameraOutputMode { + case .StillImage: + if (self.stillImageOutput == nil) { + self._setupOutputs() + } + if let validStillImageOutput = self.stillImageOutput? { + self.captureSession?.addOutput(validStillImageOutput) + } + case .VideoOnly, .VideoWithMic: + self.captureSession?.addOutput(self._getMovieOutput()) + + if newCameraOutputMode == .VideoWithMic { + if (self.mic == nil) { + self._setupMic() + } + if let validMic = self.mic? { + self.captureSession?.addInput(validMic) + } + } + } + self.captureSession?.commitConfiguration() + self.currentCameraOutputMode = newCameraOutputMode; + self._orientationChanged() + } + private func _setupOutputs() { if (self.stillImageOutput == nil) { diff --git a/camera/ViewController.swift b/camera/ViewController.swift index 46a73fc..10475e7 100644 --- a/camera/ViewController.swift +++ b/camera/ViewController.swift @@ -26,7 +26,7 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - self.cameraManager.addPreviewLayerToView(self.cameraView, newCameraOutputMode: CameraOutputMode.VideoWithMic) + self.cameraManager.addPreviewLayerToView(self.cameraView, newCameraOutputMode: CameraOutputMode.StillImage) self.cameraManager.cameraDevice = .Front self.imageView.hidden = true CameraManager.sharedInstance.showErrorBlock = { (erTitle: String, erMessage: String) -> Void in