AVAssetExportSession卡住(未启动)导出 [英] AVAssetExportSession stuck (not starting) export

查看:94
本文介绍了AVAssetExportSession卡住(未启动)导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从照片库中导出视频,但是从不执行导出回调.我会定期检查导出进度,进度始终为零.

I have attempted to export videos from Photo Library, but the export callback is never executed. I periodically check the progress of the export, and the progress is always zero.

下面的代码在99.9%的情况下都可以工作,但是有时在某些设备上(绝对随机)它会停止工作,只有重新启动iPhone才有帮助.

The code below works in 99.9% cases, but sometimes on some devices (absolutely randomly) it stops working and only restart of the iPhone helps.

AVAssetExportSession.Status 始终处于 waiting 状态


class FilesInteractor {
    static func tempDirectoryPath() -> String {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
        return documentsPath.appendingPathComponent("temp") as String
    }

    static func createTempDirectory() {
        if !FileManager.default.fileExists(atPath: tempDirectoryPath()) {
            try? FileManager.default.createDirectory(atPath: tempDirectoryPath(), withIntermediateDirectories: true, attributes: nil)
        }
    }

    static func testVideoURL(name: String, ext: String = "mov") -> URL {
        createTempDirectory()
        let outputURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("test").appendingPathComponent("\(name).\(ext)", isDirectory: false)

        log.debug("Test video URL: \(outputURL)")

        return outputURL
    }
}

import AVFoundation

let asset = AVAsset()
let outputURL = FilesInteractor.testVideoURL("output")

let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPreset1280x720)
exportSession?.outputFileType = .mov
exportSession?.outputURL = outputURL

try? FileManager.default.removeItem(at: outputURL)
exportSession?.exportAsynchronously(completionHandler: {
    print("sometimes never calls")
})

其他视频应用程序也冻结(Filto,Videoleap):

Other video apps also freeze (Filto, Videoleap):

推荐答案

我在某些Github项目中几次见到此问题,通常与URL的创建有关.不知道您在问题中输入的代码是否只是某个占位符,但我认为您应该创建这样的fileURL而不是"string".

I saw this issue a couple of times on some Github projects and usually it had something to do with how the URL was created. Not sure if the code you put in your question was just some placeholder but I think you should create a fileURL like this instead of "string".

var tempFileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("temp_video_data.mp4", isDirectory: false)
tempFileUrl = URL(fileURLWithPath: tempFileUrl.path)
exportSession.outputURL = tempFileUrl

也许这会解决?

这篇关于AVAssetExportSession卡住(未启动)导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆