swift - AVCaptureVideoDataOutput captureOutput 未被调用

标签 swift macos cocoa avfoundation

我正在使用 AVCaptureScreenInput 在 Mac 上尝试屏幕捕获,但从未调用 AVCaptureVideoDataOutput 委托(delegate) captureOutput,我不确定为什么。我确实收到一条通知,说明捕获 session 已开始。

import Cocoa
import AVFoundation

class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

    var captureSession: AVCaptureSession!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear() {
        super.viewWillAppear()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.errorNotif), name: AVCaptureSessionRuntimeErrorNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.startedNotif), name: AVCaptureSessionDidStartRunningNotification, object: nil)
        startScreenCapture()
    }

    override func viewWillDisappear() {
        super.viewWillDisappear()
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func captureOutput(captureOutput: AVCaptureOutput!, didDropSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
        print("ignore frame, add code to handle later")
    }

    func startScreenCapture() {

        let displayId = CGMainDisplayID()
        captureSession = AVCaptureSession()
        if captureSession.canSetSessionPreset(AVCaptureSessionPresetHigh) {
            captureSession.sessionPreset = AVCaptureSessionPresetHigh
        }
        let captureScreenInput = AVCaptureScreenInput(displayID: displayId)
        if captureSession.canAddInput(captureScreenInput) {
            captureSession.addInput(captureScreenInput)
        } else {
            print("Could not add main display to capture input")
        }

        let output = AVCaptureVideoDataOutput()

        let queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL)
        output.setSampleBufferDelegate(self, queue: queue)
        output.alwaysDiscardsLateVideoFrames = true

        output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as NSString: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA)]
        captureSession.addOutput(output)
        captureSession.startRunning()
    }

    func errorNotif() {
        print("error starting capture")
    }
    func startedNotif() {
        print("started screen capture")
    }
}

最佳答案


我为基本示例添加了 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())
    }
}

关于swift - AVCaptureVideoDataOutput captureOutput 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37890791/

相关文章:

Swift POST 请求不起作用

swift - 每个收集部分的水平流布局

swift - AWS S3 图片上传

python - Mac 上使用 python 3.4 的虚拟环境

c++ - 用 C++ 编写 NSTouchBar

ios - 注释 map View (自定义标记)在 swift IOS 中不起作用

C++ Qt : Static builds and external dylibs

objective-c - cocoa WebView SWF

Objective-C - 循环遍历所有已安装的应用程序

objective-c - 在 Cocoa 中,如何从 View 创建 NSImage? (与 Safari 4 中的热门站点非常相似。)