ios AVAsset不支持的URL

bxjv4tth  于 2023-07-01  发布在  iOS
关注(0)|答案(2)|浏览(115)

我试图加载从互联网下载的视频存储在文档目录中。但是当我尝试时,我不断地得到:错误:错误域=NSURLErrorDomain Code=-1002“不支持的URL”

let dstURL = URL(string: dstPath)!
let asset = AVAsset(url: dstURL)
asset.loadValuesAsynchronously(forKeys: ["tracks", "playable"]) {
    var error: NSError?
    let status = asset.statusOfValue(forKey: "tracks", error: &error)
      if error != nil {
          print("status: \(status), error: \(error!)")
      }
    }
tp5buhyn

tp5buhyn1#

您需要使用URL(fileUrlWithPath:)而不是URL(string:)

mbjcgjjk

mbjcgjjk2#

虽然您可以使用URL(fileURLWithPath:),但几乎在所有平台上都已弃用它(例如自macOS 13.3起)。您应该使用URL(string:),并确保您的URL是完整的(即有什么阴谋吗?)
作为一个例子,

guard var components = URLComponents(string: path) else {
  throw UserError.invalidPath(path)
}

// We're assuming that `path` didn't have this set (e.g. it's an absolute path and just assumes).
components.scheme = "file"

guard let url = components.url else {
  throw UserError.invalidPath(path)
}

return url

相关问题