gpt4 book ai didi

ios - 从另一个队列 AVFoundation 调度委托(delegate)回调到主队列

转载 作者:行者123 更新时间:2023-11-29 00:27:41 24 4
gpt4 key购买 nike

我正在开发一个封装在基本 View Controller 中的相机,带有用于回调的委托(delegate)样式接口(interface),因此作为客户端,我所要做的就是子类化相机 View Controller ,实现委托(delegate)方法,并添加用户界面按钮。我的问题是关于录制视频。视频录制是在一个独特的后台任务上启动的,以确保可以将录制内容写入临时文件。这是在我的private let sessionQueue = DispatchQueue(label: "com.andrewferrarone.sessionQueue") session 队列:

public func startRecording()
{
guard let movieFileOutput = self.movieFileOutput else { return }

//get video preview layer's video orientation on main queue
guard let videoPreviewLayerOrientation = self.previewView.videoPreviewLayer?.connection.videoOrientation else {
print("handleRecord: videoPreviewLayer is nil")
return
}

self.sessionQueue.async {

if !movieFileOutput.isRecording {
if UIDevice.current.isMultitaskingSupported {

self.backgroundRecordingID = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
}

//update orientation on the movie file output video connection before recording
let movieFileOutputConnection = self.movieFileOutput?.connection(withMediaType: AVMediaTypeVideo)
movieFileOutputConnection?.videoOrientation = videoPreviewLayerOrientation

//start recording to a temporary file:
let outputFileName = UUID().uuidString
let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath: outputFilePath), recordingDelegate: self)
}
}
}

所以录音被设置为一个后台任务,发送到 self.sessionQueue。当我停止录制时,我收到一个 AVCaptureFileOutputRecordingDelegate 方法。在这个方法中,我想用文件路径回调我的委托(delegate),然后进行清理。如何确保委托(delegate)在进行清理并删除临时文件之前可以保留临时文件路径中的记录?

 public func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!)
{
//cleanup func for later
func cleanup()
{
let path = outputFileURL.path

if FileManager.default.fileExists(atPath: path) {
do {
try FileManager.default.removeItem(atPath: path)
}
catch let error {
print("Could not remove file at url: \(outputFileURL), error: \(error.localizedDescription)")
}
}

if let currentBackgroundRecordingID = self.backgroundRecordingID {
self.backgroundRecordingID = UIBackgroundTaskInvalid

if currentBackgroundRecordingID != UIBackgroundTaskInvalid {
UIApplication.shared.endBackgroundTask(currentBackgroundRecordingID)
}
}
}

var success = true

if error != nil {
print("Movie file finishing error: \(error)")
success = ((error as NSError).userInfo[AVErrorRecordingSuccessfullyFinishedKey] as AnyObject).boolValue
}

DispatchQueue.main.async {
self.delegate?.camera(self, didFinishRecordingToOutputFileAt: outputFileURL, success: success)
}

cleanup()
}

因此,我将结果回调到主队列上,但随后我需要调用 cleanup() 我应该在回调我的委托(delegate)后立即在主队列上执行此操作吗?这安全吗?或者如果我保持现在的样子,那么我们就在 self.sessionQueue 上,并且我不确定在委托(delegate)方法实现有时间之前是否会发生 cleanup()保留录音。如果有人可以让我了解正在发生的事情以及在这里做什么是最安全的事情,那就太好了。根据苹果的说法,文档说不要假设在特定线程上调用 AVCaptureFileOutputRecordingDelegate didFinishRecordingToOutputFileAt 方法。感谢您的时间和帮助!

最佳答案

怎么样:

DispatchQueue.main.async {
self.delegate?.camera(self, didFinishRecordingToOutputFileAt: outputFileURL, success: success)
self.sessionQueue.async {
cleanup()
}
}

我认为这是处理这种情况的标准方法。当委托(delegate)方法完成时,您假设委托(delegate)已完成文件(或将其复制到其他地方)。

关于ios - 从另一个队列 AVFoundation 调度委托(delegate)回调到主队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42635404/

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