【问题标题】:Swift - AVAssetExportSession exportSession.exportAsynchronously completion handler not calledSwift - AVAssetExportSession exportSession.exportAsynchronously 完成处理程序未调用
【发布时间】:2020-12-27 08:47:12
【问题描述】:

我在我的项目中使用了此链接和以下代码,但 AVAssetExportSession - exportAsynchronously 方法完成处理程序未在我的项目中调用:

StackLink

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 上分享了我的项目,你可以查看它,
谢谢

GitRepo

我使用 Xcode 12.3

【问题讨论】:

  • 我认为这是 Xcode 错误,因为我签入了其他项目并且遇到了同样的问题,例如:raywenderlich.com/…

标签: swift xcode


【解决方案1】:

这是 iOS 错误,即使我的 iOS 设备上的屏幕录制也不起作用,在我知道这个错误后,我重新启动手机并且一切正常,但这需要我一些时间才能理解解决方案。

我使用的是 iOS 14.3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 2021-03-18
    • 1970-01-01
    • 2018-04-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多