我一直在做一个小项目,涉及从Web服务器下载视频文件,将所述文件复制到文档目录,然后通过AVPlayer播放。
将文件下载到文档目录不是问题。我可以下载文件并保存它没有问题。但是,当涉及到将文件加载到AVPlayer中,并且这样做我在AVPlayerViewController的实例中播放时,视频控制器会弹出它应该的,但视频不会加载。
我意识到,在模拟器中进行测试时,每次重建项目时,文档目录都会更改。这就是为什么我在播放之前检查文件是否存在的原因,虽然我知道该文件存在,但它仍然拒绝播放。
以下是我的播放器代码:
let fileName = downloadURL.characters.split("/").map(String.init).last as String!
let fileNameHD = downloadURLHD.characters.split("/").map(String.init).last as String!
let downloadFilePath = getDocumentsDirectory() + "/" + "\(fileNameHD)"
let checkValidation = NSFileManager.defaultManager()
if checkValidation.fileExistsAtPath(downloadFilePath){
print("video found")
}
let videoFile = NSURL(string:downloadFilePath)
let player = AVPlayer(URL: videoFile!)
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = self.view.frame
player.play()
self.presentViewController(playerController, animated: true) {
playerController.player!.play()
}
尝试使用AVPlayer
而不是AVPlayerViewController
,
let videoURL = NSURL(string: "your url string")
let player = AVPlayer(URL: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
并导入AVKit,导入AVFoundation。
OR
像这样的viewController,
let player = AVPlayer(URL: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
每当我们重建应用程序时,我们的文档目录路径都会发生变化。
因此您无法播放旧文档目录路径中的视频。因此,您必须保存URL的最后一个路径组件。就像您的文档目录网址在此路径下载视频后看起来像这样: -
let videoURL = "/var/mobile/Containers/Data/Application/1F6CDF42-3796-4153-B1E8-50D09D7F5894/Documents/2019_02_20_16_52_47-video.mp4"
var videoPath = ""
if let url = videoURL {
videoPath = url.lastPathComponent
}
print(videoPath)
// It will print the last path of your video url: - "2019_02_20_16_52_47-video.mp4"
现在,您可以在任何需要的位置将此路径保存在Core Database或Sqlite或User Defaults中。然后,如果你想再次播放视频。因此,您必须从保存它的位置获取此路径。
Note:- In Below function you have to pass the last path component of your video. How to call this function.
func playVideo() {
self.startVideoFrom(videoPath:"2019_02_20_16_52_47-video.mp4")
}
// Play Video from path
func startVideoFrom(videoPath: String) {
let outputMineURL = self.createNewPath(lastPath: videoPath)
let player = AVPlayer(url: outputMineURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
/// Generate the new document directory path everytime when you rebuild the app and you have to append the last component of your URL.
func createNewPath(lastPath: String) -> URL {
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let destination = URL(fileURLWithPath: String(format: "%@/%@", documentsDirectory,lastPath))
return destination
}
For more reference, you can see this question:- https://stackoverflow.com/q/47864143/5665836