didOutputSampleBuffer委托永远不会被调用(iOS / Swift 3)

时间:2017-04-28 13:53:21

标签: ios swift avcapturesession

我正在尝试跟踪视频录制的样本缓冲率。

我有一个带AVCaptureFileOutputRecordingDelegateAVCaptureVideoDataOutputSampleBufferDelegate的视图控制器,然后像这样设置缓冲区输出:

sessionQueue.async { [weak self] in
        if let `self` = self {
            let movieFileOutput = AVCaptureMovieFileOutput()

            let bufferQueue = DispatchQueue(label: "bufferRate", qos: .userInteractive, attributes: .concurrent)

            let theOutput = AVCaptureVideoDataOutput()
                theOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value:kCVPixelFormatType_32BGRA)]
                theOutput.alwaysDiscardsLateVideoFrames = true
                theOutput.setSampleBufferDelegate(self, queue: bufferQueue)

            if self.session.canAddOutput(theOutput) {
                self.session.addOutput(theOutput)
                print("ADDED BUFFER OUTPUT")
            }

            if self.session.canAddOutput(movieFileOutput) {
                self.session.beginConfiguration()
                self.session.addOutput(movieFileOutput)
                self.session.sessionPreset = AVCaptureSessionPresetHigh
                if let connection = movieFileOutput.connection(withMediaType: AVMediaTypeVideo) {
                    if connection.isVideoStabilizationSupported {
                        connection.preferredVideoStabilizationMode = .auto
                    }
                }

                self.session.commitConfiguration()

                self.movieFileOutput = movieFileOutput

                DispatchQueue.main.async { [weak self] in
                    if let `self` = self {
                        self.recordButton.isEnabled = true
                    }
                }
            }
        }
    }

此外,我已经实现了读取缓冲区的功能:

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

问题在于,当运行摄像机时,它会按照预期正确记录(代码未显示,因为它正常工作),但CaptureOutput样本缓冲区永远不会被调用。我究竟做错了什么?我认为这与我设置它的方式有关?

1 个答案:

答案 0 :(得分:5)

使用Swift 3语法确保委托签名正确。

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)

我使用的是Swift 2.3语法,编译器没有警告我这个问题。尝试输入didOut sampleBuffer并检查XCode是否自动修正了该委托方法的正确语法。