簡體   English   中英

iOS:未使用AVCaptureMovieFileOutput和AVCaptureVideoDataOutput調用captureOutput函數

[英]iOS: captureOutput function isn't called using AVCaptureMovieFileOutput and AVCaptureVideoDataOutput

我正在嘗試實現一個視圖,該視圖可以顯示后置攝像頭的預覽視頻並處理捕獲的幀。 我想使用兩個輸出:一個用於保存視頻,另一個用於處理每一幀。

let movieOutput = AVCaptureMovieFileOutput()
let videoDataOutput = AVCaptureVideoDataOutput()

我已經將委托添加到我的視圖控制器中:

class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate, AVCaptureVideoDataOutputSampleBufferDelegate

另外,我將輸出添加到AVCaptureSession中:

do {
    videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String: NSNumber(value: kCVPixelFormatType_32BGRA)]
    videoDataOutput.alwaysDiscardsLateVideoFrames = true
    let queue = DispatchQueue(label: "videosamplequeue")
    videoDataOutput.setSampleBufferDelegate(self, queue: queue)
    guard captureSession.canAddOutput(videoDataOutput) else {
        fatalError()
    }
    if captureSession.canAddOutput(videoDataOutput){
        captureSession.addOutput(videoDataOutput)
    }

    videoConnection = videoDataOutput.connection(withMediaType:AVMediaTypeVideo)
}

if captureSession.canAddOutput(movieOutput) {
    captureSession.addOutput(movieOutput)
}

我的預覽層運行完美,並且可以在UI視圖中看到圖片顯示。 但是從來沒有調用captureOutput。 如果我發表評論:

//if captureSession.canAddOutput(movieOutput) {
//        captureSession.addOutput(movieOutput)
//    }

然后,我的captureOutput被調用並且工作正常,但是我想將視頻保存在文件中。 我正在使用Swift 3,因此我正在使用:

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

目前,當你刪除其他來源,它仍然呼吁captureOutput也許用假數據,但仍然videoDataOutput,因為你設置sampleBufferDelegate它。 但是captureOutput並不適用於movieOutput

movieOutputAVCaptureMovieFileOutput ,它是AVCaptureFileOutput的子類。

AVCaptureFileOutput實現兩個協議: AVCaptureFileOutputDelegate和AVCaptureFileOutputRecordingDelegate

您應該實現它們中的一種(閱讀文檔以確定哪種適合您的要求),並實現它們的方法,並期望它們被調用,而不是將captureOutput調用兩次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM