如何使用 fileURL 播放视频
How to play video using the fileURL
我可以使用自定义录像机获取输出文件 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 class
的更多信息
所以你必须像那样访问文档目录。
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 兼容,因此您可能需要对前两行进行一些更改。
尝试在 class 范围而不是方法范围内声明 let playerViewController = AVPlayerViewController()
。我的猜测是,它被释放了。
Kamarshad 的观点再次是正确的。您应该从 fileManager 而不是应用程序 Bundle
中检索 URL
我不是那么聪明,我什至不确定为什么会收到这个,抱歉我在手机上 phone n 我知道我被 Mac OS 每次我登录 gmail 帐户时都会以某种方式继续登录的设备。我收到一条消息,指出 Mac 设备已在我登录 phone 特定 gmail 帐户的那一刻精确登录。当我退出一堆帐户时,我收到一条消息说“让这个 Mac 设备登录到你的 Google 帐户”我不知道发生了什么或者这是什么
我可以使用自定义录像机获取输出文件 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 class
所以你必须像那样访问文档目录。
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 兼容,因此您可能需要对前两行进行一些更改。
尝试在 class 范围而不是方法范围内声明 let playerViewController = AVPlayerViewController()
。我的猜测是,它被释放了。
Kamarshad 的观点再次是正确的。您应该从 fileManager 而不是应用程序 Bundle
中检索 URL我不是那么聪明,我什至不确定为什么会收到这个,抱歉我在手机上 phone n 我知道我被 Mac OS 每次我登录 gmail 帐户时都会以某种方式继续登录的设备。我收到一条消息,指出 Mac 设备已在我登录 phone 特定 gmail 帐户的那一刻精确登录。当我退出一堆帐户时,我收到一条消息说“让这个 Mac 设备登录到你的 Google 帐户”我不知道发生了什么或者这是什么