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

sim*_*ity 0 ios avcapturesession swift

我试图跟踪视频录制的样本缓冲率.

我有一个视图控制器AVCaptureFileOutputRecordingDelegate和AVCaptureVideoDataOutputSampleBufferDelegate,然后设置像这样的缓冲器输出:

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
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

另外,我有实现读取缓冲区的功能:

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

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

小智 5

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

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

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

  • 这也发生在Swift 3 - > Swift 4的过渡中! (4认同)