   }
 
   @IBAction func toggleRecording(_ sender: Any) {
-    shouldRecord = recordButton.state == NSOnState
+    switch recordButton.state {
+    case NSOnState:
+      beginRecording()
+    default:
+      endRecording()
+    }
+  }
+
+  func beginRecording() {
+    DispatchQueue.global().async {
+      self.captureSession.startRunning()
+      let url = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Desktop/recording.mov")
+      self.movieFileOutput.startRecording(toOutputFileURL: url, recordingDelegate: self)
+    }
+  }
+
+  func endRecording() {
+    movieFileOutput.stopRecording()
   }
 }
 

    return false
  }
}

import AVFoundation
import Cocoa

class ViewController: NSViewController {

  let captureSession = AVCaptureSession()
  let movieFileOutput = AVCaptureMovieFileOutput()

  @IBOutlet var textField: NSTextField!
  @IBOutlet var label: NSTextField!
