AVCaptureVideoDataOutput captureOutput not being called

后端 未结 2 913
臣服心动
臣服心动 2021-01-14 17:55

I\'m trying screen capture on a Mac with AVCaptureScreenInput, but AVCaptureVideoDataOutput delegate captureOutput is never called, and I\'m not sure why. I do get a notific

相关标签:
2条回答
  • 2021-01-14 18:36

    You need to define the didOutputSampleBuffer delegate callback to actually receive the captured frames:

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
        print("captured \(sampleBuffer)")
    }
    

    p.s. I'm not sure about macOS, but viewWillAppear may not be a good place to do initialisation because on iOS at least it can be called multiple times.

    0 讨论(0)
  • 2021-01-14 18:38

    I added for basic example AVCaptureVideoDataOutputSampleBufferDelegate

    class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {    
    
    
        let videoQueue = DispatchQueue(label: "VIDEO_QUEUE")
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let captureSession = AVCaptureSession()
    
            guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
    
            guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    
            captureSession.addInput(input)
    
            captureSession.startRunning()
    
            let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            view.layer.addSublayer(previewLayer)
            previewLayer.frame = view.frame
    
            let dataOutput = AVCaptureVideoDataOutput()
            dataOutput.setSampleBufferDelegate(self, queue: videoQueue)
            captureSession.addOutput(dataOutput)
        }
    
        func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
            print("Camera was able to capture a frame:", Date())
        }
    }
    
    0 讨论(0)
提交回复
热议问题