gpt4 book ai didi

swift - AVCaptureMovieFileOutput 从不调用屏幕录制上的委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 10:27:35 24 4
gpt4 key购买 nike

我能够组装一个最小的屏幕录制 Swift 演示,用于调试另一件事,但有些东西告诉我 - AVFoundation 从未调用我的委托(delegate)进行录制。事实上,他们都不是。

代码非常简单:

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() 即可毫无问题地执行录制,并在录制完成后几秒钟后保存文件,但“已调用录制委托(delegate)” 永远不会被打印:

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

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

最佳答案

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

什么不起作用是这样的:

let delegate = BrandNewObject()

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

不知何故,这样做委托(delegate)会被忽略。也许该对象已被清除?

关于swift - AVCaptureMovieFileOutput 从不调用屏幕录制上的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59818119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com