问题描述
我在我的项目中使用了此链接和以下代码,但 AVAssetExportSession - exportAsynchronously 方法完成处理程序未在我的项目中调用:
I used this link and following code in my project but AVAssetExportSession - exportAsynchronously method completion handler doesn't called in my project:
func encodeVideo(at videoURL: URL, completionHandler: ((URL?, Error?) -> ())?) {
let avAsset = AVURLAsset(url: videoURL, options: nil)
let startDate = Date()
//Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
}
//Creating temp path to save the converted video
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory.appendingPathComponent("rendered-Video.mp4")
//Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: filePath.path) {
do {
try FileManager.default.removeItem(at: filePath)
} catch {
completionHandler?(nil, error)
}
}
exportSession.outputURL = filePath
exportSession.outputFileType = .mp4
exportSession.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
exportSession.timeRange = range
exportSession.exportAsynchronously {
switch exportSession.status {
case .failed:
print(exportSession.error ?? "NO ERROR")
completionHandler?(nil, exportSession.error)
case .cancelled:
print("Export canceled")
completionHandler?(nil, nil)
case .completed:
//Video conversion finished
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession.outputURL ?? "NO OUTPUT URL")
completionHandler?(exportSession.outputURL, nil)
case .unknown:
print("Export Unknown Error")
default: break
}
}
}
我也在GitHub上分享了我的项目,你可以查看,
谢谢
I also share my project on GitHub that you can check it,
thanks
我使用 Xcode 12.3
I use Xcode 12.3
推荐答案
这是 iOS 错误,即使我的 iOS 设备上的屏幕录制不起作用,在我知道这个错误后,我重新启动手机,一切正常,但这需要我有时会理解解决方案.
it was iOS bug even screen recording on my iOS device doesn't work, after i know this bug, I restart my phone and everything goes fine but this takes me some times to understand the solution.
我使用的是 iOS 14.3
I'm using iOS 14.3
这篇关于Swift - AVAssetExportSession exportSession.exportAsynchronously 完成处理程序未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!