【发布时间】:2017-01-10 10:27:46
【问题描述】:
我正在尝试播放下载到设备文档目录的视频。由于某种原因,它不会发生。我得到的只是这个符号:
这是我的代码:
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let videoFileUrl = URL(fileUrlWithPath: documentPath)
let videoFilePath = videoFileUrl.appendingPathComponent("somVideo.mp4").path
if FileManager.default.fileExists(atPath: videFilePath){
let player = AVPlayer(url: URL(string: videoFilePath)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
else {
print("File doesn't exist")
}
该文件存在于该位置 - 我已经检查了模拟器的文档目录 - 并且 else 语句没有被触发。控制台不会打印任何其他内容。
任何帮助将不胜感激。
【问题讨论】:
-
您将
videoFileUrl传递给AVPlayer。请改用URL(string: videoFilePath)!。videoFileUrl指的是文档目录,而videoFilePath指的是文件本身。 -
Oups - 这是一个错字 - 我已经修复了代码。
-
永远不要在文件系统中使用
URL(string:作为字符串路径。您必须使用URL(fileURLWithPath:(带有大写字母的URL!),因为只有这个初始化程序添加了file://方案。无论如何,从 URL 的路径重新创建 URL 并不是很有用。
标签: ios iphone swift3 avfoundation avkit