Can't Access Camera on Xcode Playgrounds

I tried to access my mac's front-facing camera using the code below. It worked in Swift Playgrounds but not in Xcode Playground.

import AVFoundation
import PlaygroundSupport

//change uiview size dynamically, access video from avcapture

class CameraView: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate{
  var captureSession: AVCaptureSession?
  var previewLayer: AVCaptureVideoPreviewLayer?
  var previewView: UIView!
  var output = AVCaptureVideoDataOutput() //video output
  override func viewDidLoad(){
    super.viewDidLoad()
    setUpCamera()
  }
   
  func setUpCamera(){
    captureSession = AVCaptureSession()
    previewView = UIView(frame: UIScreen.main.bounds)
    view.addSubview(previewView)
    if let cameraDevice = AVCaptureDevice.default(for: .video){
      do{
        let input = try AVCaptureDeviceInput(device: cameraDevice)
        if captureSession!.canAddInput(input){
          captureSession!.addInput(input)
        }
        if captureSession!.canAddOutput(output){
          output.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
          captureSession!.addOutput(output)
          previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
          previewLayer!.videoGravity = AVLayerVideoGravity.resize
          previewLayer!.connection?.videoOrientation = .portrait
          let viewLayer: CALayer = self.previewView.layer
          view.bounds = UIScreen.main.bounds
          previewLayer!.frame = view.bounds
          viewLayer.addSublayer(self.previewLayer!)
          captureSession!.startRunning()
        }
         
      }catch{
        print(error)
      }
       
    }
  }
   
}
PlaygroundPage.current.liveView = CameraView()

what do you mean by "does not work"? your code doesn't compile, primarily because UIKit is not available on macOS (AppKit is, so your UIViews should be NSViews). Even so, in setupCamera you refer to a variable called 'view' which is not declared.

Can't Access Camera on Xcode Playgrounds
 
 
Q