ios - 文档目录中的 AVPlayer 和视频

标签 ios swift avplayer

我一直在从事一个小项目,该项目涉及从网络服务器下载视频文件,将所述文件复制到文档目录,然后通过 AVPlayer 播放它。

将文件下载到文档目录并不是问题。我可以毫无问题地下载该文件并保存它。但是,当将文件加载到 AVPlayer 中时,并且在执行此操作时,我在 AVPlayerViewController 的实例中播放该文件,视频 Controller 会按预期弹出,但视频不会加载。

我意识到,在模拟器中测试时,每次重建项目时文档目录都会发生变化。这就是为什么我在播放之前检查文件是否存在,尽管我知道文件存在,但它仍然拒绝播放。

这是我的播放器代码:

     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()
        }

最佳答案

每次我们重建应用程序时,我们的文档目录路径都会发生变化。

因此您无法播放旧文档目录路径中的视频。因此,您必须保存 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"

现在将此路径保存在核心数据库或 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

关于ios - 文档目录中的 AVPlayer 和视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36629056/

相关文章:

ios - 设置值 :ForKey: instead of set(whatever)?

ios - 在 Swift 中创建可变参数方法

iOS:AVPlayer 视频预加载

ios - 如何在 iOS 的 UITableViewCell 中播放视频(自动播放)

ios - 如何用 NSNumbers 做数学运算

php - NSURL请求:EXC_BAD_ACCESS

ios - AVPlayerViewController 周围的奇怪边框

ios - 如何在 iOS 中的 HLS(m3u8) 播放期间找出当前 TS 段?

ios - 在 iOS 中录制音频并永久保存

arrays - 从 [PFObject] 沮丧?到 [PFObject] 只解包选项,你的意思是使用 "!"