我正在尝试使用AVFoundation
导出资产。我有exportSession设置的代码,如果放在viewDidLoad()
中它可以正常工作。但是,如果我将逻辑放在按下按钮时调用的函数中,会话将失败,并显示未知错误。
以下是我正在使用的代码:
@objc func uploadVideo() {
guard let url = self.videoUrl else { return }
var asset = AVURLAsset(url: url)
let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith: asset)
if compatiblePresets.contains(AVAssetExportPresetLowQuality) {
let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetLowQuality)
let outputUrl = getDocumentsDirectory().appendingPathComponent("testing.mp4")
exportSession?.outputURL = outputUrl
exportSession?.outputFileType = AVFileType.mp4
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.exportAsynchronously(completionHandler: {
guard let status = exportSession?.status else { return }
switch status {
case AVAssetExportSessionStatus.failed:
print(exportSession?.error)
break
case AVAssetExportSessionStatus.cancelled:
break
case AVAssetExportSessionStatus.completed:
print("Exported succesfully...")
break
default:
break
}
})
}
}
调用此方法时,我在控制台中收到以下错误:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x10b41b400 {Error Domain=NSOSStatusErrorDomain Code=-16979 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16979)
我似乎无法弄清问题是什么。非常感谢任何帮助。