我一直致力于一个小项目,涉及从网络服务器下载视频文件,将所述文件复制到文档目录,然后通过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()
}
答案 0 :(得分:0)
尝试使用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()
答案 1 :(得分:0)
每次我们重建应用程序时,我们的文档目录路径都会更改。
因此,您无法从旧文档目录路径播放视频。因此,您不必保存URL的最后一个路径部分。像您的文档目录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或“用户默认值”中的任意位置。然后,如果您想再次播放视频。因此,您必须从保存路径获取此路径。
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