10
votes

Is it possible to display a camera feed in a macOS app? I'm basically trying to capture images frame by frame from a camera feed. I know how to do this in iOS, but for some reasons, on macOS, I can't seem to find any AVCaptureDevice for video.

let x = AVCaptureDevice.devices()

// When I print x, I only see the Microphone as the only capture device. No reference to video/camera whatsoever

guard let device = AVCaptureDevice.default(for: .video) else{ return }
2
I would love to help. Could you please edit your question to make it a little more clear as to what you're trying to accomplish? I am a tad confused and would like some elaboration. - Colin
Hi Colton, what can I elaborate on? AVCaptureDevice.devices() returns a capture device object for video on iOS, but not on macOS. Why? - 7ball
@Colton only on iOS - pvg
@7ball please take a look at my answer I hope this helps. - Colin
I have corrected my answer - Colin

2 Answers

20
votes

I had the exact same issue, AVCaptureDevice.devices() only returned one entry, a microphone. Solution for me was to turn on the Camera checkbox in Project:Capabilities:App Sandbox. Output after I set the Camera checkbox...

▿ 2 elements
  - 0 : <AVCaptureHALDevice: 0x60c0000e4980 [Built-in Microphone][AppleHDAEngineInput:1B,0,1,0:1]>
  - 1 : <AVCaptureDALDevice: 0x101302b10 [FaceTime HD Camera][CC245055EWDF6VVDY]>
-2
votes

To find available capture devices use AVCaptureDevice.DiscoverySession as documented here

Then use the default(_:for:position:) method to return a default device for the specified media type as documented here