Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AVCameraViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- initializeMotionManager()
- sessionQueue.async {
- let movieFileOutput = AVCaptureMovieFileOutput()
- if self.session.canAddOutput(movieFileOutput) {
- self.session.beginConfiguration()
- self.session.addOutput(movieFileOutput)
- self.session.sessionPreset = .high
- if let connection = movieFileOutput.connection(with: .video) {
- if connection.isVideoStabilizationSupported {
- connection.preferredVideoStabilizationMode = .auto
- }
- }
- self.session.commitConfiguration()
- movieFileOutput.maxRecordedDuration = CMTime(seconds: 120, preferredTimescale: 60)
- self.movieFileOutput = movieFileOutput
- DispatchQueue.main.async {
- self.recordButton.isEnabled = true
- }
- }
- }
- }
- func fileOutput(_ output: AVCaptureFileOutput,
- didFinishRecordingTo outputFileURL: URL,
- from connections: [AVCaptureConnection],
- error: Error?) {
- // Note: Since we use a unique file path for each recording, a new recording won't overwrite a recording mid-
- save.
- UIApplication.shared.isIdleTimerDisabled = false
- func cleanup() {
- let path = outputFileURL.path
- if FileManager.default.fileExists(atPath: path) {
- do {
- try FileManager.default.removeItem(atPath: path)
- } catch {
- print("Could not remove file at url: (outputFileURL)")
- }
- }
- if let currentBackgroundRecordingID = backgroundRecordingID {
- backgroundRecordingID = UIBackgroundTaskIdentifier.invalid
- if currentBackgroundRecordingID != UIBackgroundTaskIdentifier.invalid {
- UIApplication.shared.endBackgroundTask(currentBackgroundRecordingID)
- }
- }
- }
- var success = true
- if error != nil {
- print("Movie file finishing error: (String(describing: error))")
- success = (((error! as NSError).userInfo[AVErrorRecordingSuccessfullyFinishedKey] as AnyObject).boolValue)!
- }
- if success {
- // Check authorization status.
- UIView.animate(withDuration: 0.5){
- self.overlay.alpha = 0.9
- self.navigationController?.navigationBar.isTranslucent = false
- }
- footageURL = outputFileURL
- performSegue(withIdentifier: "TrimFootage", sender: nil)
- } else {
- cleanup()
- }
- // Enable the Camera and Record buttons to let the user switch camera and start another recording.
- DispatchQueue.main.async {
- // Only enable the ability to change camera if the device has more than one camera.
- self.recordButton.isEnabled = true
- // self.recordButton.setImage(#imageLiteral(resourceName: "CaptureVideo"), for: [])
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement