Can I use front camera in Xcode playgrounds Mac?

Hey guys, is it possible to use the font facing mac camera from xcode playgrounds in mac? I am trying to access the camera but i am unable to do so. The code works fine in Xcode but not in Xcode playgrounds. My app relies on the front facing camera and also I do not have an iPad to work on this project. This is the code I am using to access the camera:


let captureSession = AVCaptureSession()

var captureDevice: AVCaptureDevice!


func prepareCamera() {

captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

beginSession()

}

func beginSession() {

do {

let input = try AVCaptureDeviceInput(device: captureDevice)

captureSession.addInput(input)

} catch {

}

captureSession.beginConfiguration()

captureSession.sessionPreset = .vga640x480

let dataOutput = AVCaptureVideoDataOutput()

dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]

dataOutput.alwaysDiscardsLateVideoFrames = true

if captureSession.canAddOutput(dataOutput) {

captureSession.addOutput(dataOutput)

}

captureSession.commitConfiguration()

let queue = DispatchQueue(label: "captureQueue")

dataOutput.setSampleBufferDelegate(self, queue: queue)

captureSession.startRunning()

}


Any help would be appreciated.

Replies

The code works fine in Xcode

What do you mean by this? Are you talking about putting the code in an app project and then running that app on the Mac?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"