首页 > 解决方案 > AVCaptureMovieFileOutput 从不调用屏幕录制的委托

问题描述

我能够组装一个最小的 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()即可毫无问题地执行录制,并在录制完成后几秒钟后保存文件,但从不打印“Recording delegate WAS CALLED” :

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

只是为了确保我验证了使用具有相同结果的断点。谁能指出我的任何方向?我来回翻阅文档,但找不到任何原因(我也尝试了单独的对象和其他委托回调)没有调用委托方法。

标签: swiftmacosavfoundation

解决方案


我终于设法解决了这个问题。实际上,问题中发布的示例是正确且有效的。

这是行不通的:

let delegate = BrandNewObject()

self.fileOut!.startRecording(to: self.getTemp(), recordingDelegate: delegate)

不知何故,委托被忽略了。也许对象被清除了?


推荐阅读