AVCaptureMovieFileOutput从不在屏幕记录中调用委托

问题描述 投票:0回答:1

我能够汇编一个最小的Swift屏幕快照演示来调试另一件事,但是却使我感到困惑-AVFoundation从未调用我的委托进行录制。实际上都不是。

代码非常简单:

class ExampleRecorder: NSObject, AVCaptureFileOutputRecordingDelegate {
   private var session: AVCaptureSession?;
   private var fileOut: AVCaptureMovieFileOutput?;

   func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
       print("Recording delegate WAS CALLED")
   }

   func record() {
       self.session = AVCaptureSession()
       self.session!.beginConfiguration() //As per Apple docs for atomic config updates

       // Wire input
       let captureInput = AVCaptureScreenInput(displayID: CGMainDisplayID())
       guard self.session!.canAddInput(captureInput!) else {
           assertionFailure("Failed to add input")
           return
       }
       self.session!.addInput(captureInput!)

       // Wire output
       self.fileOut = AVCaptureMovieFileOutput()
       guard self.session!.canAddOutput(self.fileOut!) else {
           assertionFailure("Failed to add output")
           return
       }
       session!.addOutput(self.fileOut!)

       // Just check the wiring
       print(self.session!.connections)

       self.session!.commitConfiguration()

       // This should be blocking and ACTUALLY start the session as per
       // https://developer.apple.com/documentation/avfoundation/avcapturesession
       self.session!.startRunning()
       print("Is session running? \(self.session!.isRunning)")

       self.fileOut!.startRecording(to: self.getTemp(), recordingDelegate: self)
       print("Is recording running? \(self.fileOut!.isRecording)")

       //Simply stop the recording after 5 seconds
       Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(stopRecord(_:)), userInfo: nil, repeats: false)
   }

   //Nothing to see here - boilerplate to create UNIQUE temporary file
   private func getTemp() -> URL
   {
       let tempName = NSUUID().uuidString
       let tempPath = (NSTemporaryDirectory() as NSString).appendingPathComponent((tempName as NSString).appendingPathExtension("mov")!)

       print("Temp path: \(tempPath)")

       return URL(fileURLWithPath: tempPath)
   }

   @objc func stopRecord(_ timer: Timer?)
   {
       print("Is session running after 5 sec? \(self.session!.isRunning)")
       print("Is record running after 5 sec? \(self.fileOut!.isRecording)")

       fileOut?.stopRecording()
   }
}

简单地调用ExampleRecorder().record()即可进行记录,没有问题,并且在记录结束后几秒钟后保存了文件,但是“记录委托已被调用”从未打印:

2020-01-20 00:37:10.764852-0600 HEVCRecorder[29875:2886265] Metal API Validation Enabled
[<AVCaptureConnection: 0x60000026f160 [type:vide][enabled:1][active:1]>]
2020-01-20 00:37:12.061411-0600 HEVCRecorder[29875:2886265] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x6000002775e0> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
Is session running? true
Temp path: /var/folders/mv/tlzj_p0d4b9cgm854s3n73zc0000gn/T/DFCC195F-2415-4F80-AC21-25C1DB0CD181.mov
Is recording running? false
2020-01-20 00:37:12.295775-0600 HEVCRecorder[29875:2886688] GVA info: preferred scaler idx 1
2020-01-20 00:37:12.299733-0600 HEVCRecorder[29875:2886688] GVA encoder info: recomputed for fps: 15.000000, gop size in pics: 15, gop size in sec: 1.000000
GVA encoder info: recomputed for fps: 15.000000, gop size in pics: 15, gop size in sec: 1.000000 
2020-01-20 00:37:12.582543-0600 HEVCRecorder[29875:2886700] GVA info: preferred scaler idx 1
2020-01-20 00:37:12.586830-0600 HEVCRecorder[29875:2886700] GVA encoder info: recomputed for fps: 15.000000, gop size in pics: 15, gop size in sec: 1.000000
GVA encoder info: recomputed for fps: 15.000000, gop size in pics: 15, gop size in sec: 1.000000 
Is session running after 5 sec? true
Is record running after 5 sec? true

只需确保我已验证使用具有相同结果的断点。谁能指出我的方向?我来回浏览文档,但找不到任何原因,为什么没有调用任何[委托方法。

swift macos avfoundation
1个回答
0
投票
不是的工作是这样的:

let delegate = BrandNewObject() self.fileOut!.startRecording(to: self.getTemp(), recordingDelegate: delegate) 以某种方式执行此委托将被忽略。也许物体被清除了?

© www.soinside.com 2019 - 2024. All rights reserved.