AVCaptureMovieFileOutput永远不会在屏幕录制中调用委托 [英] AVCaptureMovieFileOutput never calls delegate on screen recording

查看:100
本文介绍了AVCaptureMovieFileOutput永远不会在屏幕录制中调用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够汇编一个最小的Swift屏幕快照演示来调试另一件事,但某些事情使我感到烦恼- AVFoundation 从未调用我的委托进行录制.实际上他们都不是.

I was able to assemble a minimal Swift demo of screen recording for debugging another thing but something struct me - AVFoundation never calls my delegate for recording. None of them in fact.

代码很简单:

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()即可执行录制,而不会出现问题,并且在录制完成后几秒钟即可保存文件,但录制委托已被调用" em>从不打印:

Simply calling ExampleRecorder().record() performs the recording without problems and saves the file after couple of seconds after the recording is finished but the "Recording delegate WAS CALLED" is never printed:

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

请确保我已验证是否使用具有相同结果的断点.谁能指出我的方向?我在文档中四处走动,但找不到任何原因导致为什么没有(我尝试了一个单独的对象和其他委托回调)被调用的委托方法.

Just to be sure I verified that using a breakpoint with the same results. Can anyone point me in any direction? I went back and forth over the documentation and I cannot find any reasons why none (I attempted a separate object and other delegate callbacks too) of the delegate methods are called.

推荐答案

我终于设法解决了这个问题.实际上,问题中张贴的示例正确且有效.

I finally managed to solve the problem. In fact the example posted in the question is correct and working.

无效的工作原理是:

let delegate = BrandNewObject()

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

以某种方式执行此委托将被忽略.也许物体被清除了?

Somehow doing this the delegate is ignored. Perhaps the object is purged?

这篇关于AVCaptureMovieFileOutput永远不会在屏幕录制中调用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆