gpt4 book ai didi

ios - 如何使用 fileURL 播放视频

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:59 26 4
gpt4 key购买 nike

我可以使用自定义录像机获取输出文件 URL,如下所示。

file:///var/mobile/Containers/Data/Application/5EB769B8-193F-4C9F-982A-48892333A8F0/Documents/2018-Oct-31%2012:16:54output.mov

我需要使用 AVPlayerViewController 播放该视频。我正在使用以下代码在我的 viewcontroller 中播放视频。但它不起作用。谁能指导我完成这项任务?

func pathVideo()

{


if let path = Bundle.main.path(forResource: "file:///var/mobile/Containers/Data/Application/5EB769B8-193F-4C9F-982A-48892333A8F0/Documents/2018-Oct-31%2012:16:54output.mov", ofType: "mov", inDirectory: "file:///var/mobile/Containers/Data/Application/5EB769B8-193F-4C9F-982A-48892333A8F0/Documents/2018-Oct-31%2012:16:54output.mov")

{


let video = AVPlayer(url: URL(fileURLWithPath: path))



let playerViewController = AVPlayerViewController()


playerViewController.player = video

present(playerViewController, animated: true, completion:
{

video.play()

})

}

}

最佳答案

您的代码有几处错误。正如您提到的,您将视频放置在您的应用程序文档目录中(通过查看您的视频文件路径),因此您无法通过调用 Bundle.main.path 方法来获取路径。它用于检索应用程序本地捆绑资源的路径(您放置在构建应用程序之前的文件)。有关 bundle 的更多信息类

所以你必须像那样访问文档目录。

let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! //This piece of code will return path of document directory."
let videoPath = documentsPath.appendPathComponent("2018-Oct-31%2012:16:54output.mov").path //Append your video name so that complete path could be prepared and access path property.

现在您可以使用此视频路径并传递给 URL

let video  = AVPlayer(url: URL(fileURLWithPath: videoPath))

我想你已经明白我的意思了,我希望它可以帮助你修复代码。

注意:我编写的代码片段与 Swift 4.0 兼容,因此您可能需要对前两行进行一些更改。

关于ios - 如何使用 fileURL 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53078949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com