SHARE
TWEET

Untitled

a guest Jun 16th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class AVCameraViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {
  2.  
  3.     override func viewDidAppear(_ animated: Bool) {
  4.     super.viewDidAppear(animated)
  5.     initializeMotionManager()
  6.     sessionQueue.async {
  7.         let movieFileOutput = AVCaptureMovieFileOutput()
  8.         if self.session.canAddOutput(movieFileOutput) {
  9.             self.session.beginConfiguration()
  10.             self.session.addOutput(movieFileOutput)
  11.             self.session.sessionPreset = .high
  12.             if let connection = movieFileOutput.connection(with: .video) {
  13.                 if connection.isVideoStabilizationSupported {
  14.                     connection.preferredVideoStabilizationMode = .auto
  15.                 }
  16.             }
  17.             self.session.commitConfiguration()
  18.             movieFileOutput.maxRecordedDuration = CMTime(seconds: 120, preferredTimescale: 60)
  19.  
  20.             self.movieFileOutput = movieFileOutput
  21.  
  22.             DispatchQueue.main.async {
  23.                 self.recordButton.isEnabled = true
  24.                 }
  25.             }
  26.         }
  27.     }
  28.  
  29.     func fileOutput(_ output: AVCaptureFileOutput,
  30.                 didFinishRecordingTo outputFileURL: URL,
  31.                 from connections: [AVCaptureConnection],
  32.                 error: Error?) {
  33.     // Note: Since we use a unique file path for each recording, a new recording won't overwrite a recording mid-
  34.  
  35. save.
  36.         UIApplication.shared.isIdleTimerDisabled = false
  37.         func cleanup() {
  38.             let path = outputFileURL.path
  39.             if FileManager.default.fileExists(atPath: path) {
  40.                 do {
  41.                     try FileManager.default.removeItem(atPath: path)
  42.                 } catch {
  43.                     print("Could not remove file at url: (outputFileURL)")
  44.                 }
  45.             }
  46.  
  47.             if let currentBackgroundRecordingID = backgroundRecordingID {
  48.                 backgroundRecordingID = UIBackgroundTaskIdentifier.invalid
  49.  
  50.                 if currentBackgroundRecordingID != UIBackgroundTaskIdentifier.invalid {
  51.                     UIApplication.shared.endBackgroundTask(currentBackgroundRecordingID)
  52.                 }
  53.             }
  54.         }
  55.  
  56.         var success = true
  57.  
  58.         if error != nil {
  59.             print("Movie file finishing error: (String(describing: error))")
  60.             success = (((error! as NSError).userInfo[AVErrorRecordingSuccessfullyFinishedKey] as AnyObject).boolValue)!
  61.         }
  62.  
  63.         if success {
  64.             // Check authorization status.
  65.             UIView.animate(withDuration: 0.5){
  66.                 self.overlay.alpha = 0.9
  67.                 self.navigationController?.navigationBar.isTranslucent = false
  68.             }
  69.             footageURL = outputFileURL
  70.             performSegue(withIdentifier: "TrimFootage", sender: nil)
  71.         } else {
  72.             cleanup()
  73.         }
  74.  
  75.         // Enable the Camera and Record buttons to let the user switch camera and start another recording.
  76.         DispatchQueue.main.async {
  77.             // Only enable the ability to change camera if the device has more than one camera.
  78.             self.recordButton.isEnabled = true
  79. //            self.recordButton.setImage(#imageLiteral(resourceName: "CaptureVideo"), for: [])
  80.         }
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top