title: How to record a video with avfoundation in Swift?
body: <p>I am trying to figure out how to record a video using AVFoundation in Swift. I have got as far as creating a custom camera but I only figured out how to take still pictures with it and I can't figure out how to record video. From what I understand you have to use <code>AVCaptureVideoDataOutput</code> to get the data from the recording but I can't figure out how to start the recording and implement the delegate methods.</p>
  
  <p>The whole AVFoundation Programing Guide/Still and Video Media Capture is in Objective-C and I can't seem to decipher it out. Here's my attempt to accomplish this task:</p>
  
  <p>First I set up the camera/capture session</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      captureSession.sessionPreset = AVCaptureSessionPresetHigh
      let devices = AVCaptureDevice.devices()
      for device in devices {
          if (device.hasMediaType(AVMediaTypeVideo)) {
              if(device.position == AVCaptureDevicePosition.Back) {
                  captureDevice = device as? AVCaptureDevice
                  if captureDevice != nil {
                      beginSession()
                  }
              }
          }
      }
  
  }
  </code></pre>
  
  <p>Then once beginSession() is called I set up the live feed</p>
  
  <pre><code>func beginSession() {
      var err : NSError? = nil
      captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &amp;err))
      if err != nil {
          println("error: \(err?.localizedDescription)")
      }
      previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
      self.cameraView.layer.addSublayer(previewLayer)
      self.cameraView.bringSubviewToFront(takePhotoButton)
      self.cameraView.bringSubviewToFront(self.snappedPicture)
      self.cameraView.bringSubviewToFront(self.backButton)
      previewLayer?.frame = self.cameraView.layer.frame
      captureSession.startRunning()
  }
  </code></pre>
  
  <p>Here's where I get stuck where the user presses record to actually record and capture a video:</p>
  
  <pre><code>@IBAction func takeVideoAction(sender: AnyObject) {
  
      var recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
  
      var videoFileOutput = AVCaptureMovieFileOutput()
      self.captureSession.addOutput(videoFileOutput)
  
      let filePath = NSURL(fileURLWithPath: "filePath")
  
      videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate)
  
  }
  </code></pre>
  
  <p>Then I call <code>self.videoFileOutput.stopRecording()</code>.</p>
  
  <p>Then lastly the delegate methods</p>
  
  <pre><code>func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
      return
  }
  
  func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
      return
  }
  </code></pre>
  
  <p>And nothing happens as you can imagine because I can't figure out how to implement it correctly.</p>