Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishikawakatsumi/14b240f8d427d3dc0f7a6df15215e5a4 to your computer and use it in GitHub Desktop.
Save kishikawakatsumi/14b240f8d427d3dc0f7a6df15215e5a4 to your computer and use it in GitHub Desktop.
import Cocoa
import AVFoundation
class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
private let session = AVCaptureSession()
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true
AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: .video, position: .unspecified).devices.forEach {
do {
let input = try AVCaptureDeviceInput(device: $0) // 他にもプラグインがある場合は適当に調整する
if session.canAddInput(input) {
session.addInput(input)
}
} catch {
print(error)
}
}
let previewLayer = AVCaptureVideoPreviewLayer()
previewLayer.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
previewLayer.session = session
if let layer = view.layer {
previewLayer.frame = layer.bounds
layer.addSublayer(previewLayer)
}
session.startRunning()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment