【问题标题】:Why can I play videos from my app bundle but not videos that were downloaded to the file system?为什么我可以播放我的 app bundle 中的视频,但不能播放下载到文件系统的视频?
【发布时间】:2020-01-21 21:43:05
【问题描述】:

我的目标是读取我下载并存储到我的 iOS 设备文件系统的视频媒体。不幸的是,视频播放器因以下代码而停止:

    @IBAction func playVideo(_ sender: UIButton) {
        if let video = detailItem {
            do {
                let url = try FileManager.default.url(for: .documentDirectory,
                                                      in: .userDomainMask,                                                                       
                                                      appropriateFor: nil,
                                                      create: false)
                  .appendingPathComponent(video.uuid)
                debugPrint("url: \(url)")

                // Create an AVPlayer, passing it the HTTP Live Streaming URL.
                let player = AVPlayer(url: url)

                // Create a new AVPlayerViewController and pass it a reference to the player.
                let controller = AVPlayerViewController()
                controller.player = player

                // Modally present the player and call the player's play() method when complete.
                present(controller, animated: true) {
                    player.play()
                }
            } catch {
                print("Error: \(error)")
            }
        }
    }

我认为这是视频格式问题,并且我的视频的编解码器不受支持。

但是,当我在应用程序中捆绑完全相同的视频并切换到此代码时,一切正常:

    @IBAction func playVideo(_ sender: UIButton) {
        let url = Bundle.main.url(forResource: "myvideo", withExtension: "mp4")!

//        if let video = detailItem {
            do {
//                let url = try FileManager.default.url(for: .documentDirectory,
//                                                                        in: .userDomainMask,
//                                                                        appropriateFor: nil,
//                    create: false).appendingPathComponent(video.uuid)
                debugPrint("url: \(url)")

                // Create an AVPlayer, passing it the HTTP Live Streaming URL.
                let player = AVPlayer(url: url)

                // Create a new AVPlayerViewController and pass it a reference to the player.
                let controller = AVPlayerViewController()
                controller.player = player

                // Modally present the player and call the player's play() method when complete.
                present(controller, animated: true) {
                    player.play()
                }
            } catch {
                print("Error: \(error)")
            }
//        }
    }

我不知道我在这里缺少什么并且可以得到一些帮助。 ????

编辑:负责下载的代码在这里:

        let downloadTask = URLSession.shared.downloadTask(with: video.downloadURL, completionHandler: { (tempPathURL, urlResponse, error) in
            guard let tempPathURL = tempPathURL else {
                return
            }

            do {
                let documentsDirectoryURL = try FileManager.default.url(for: .documentDirectory,
                                                                        in: .userDomainMask,
                                                                        appropriateFor: nil,
                                                                        create: false)
                try FileManager.default.moveItem(at: tempPathURL, to: documentsDirectoryURL.appendingPathComponent(video.uuid))
            } catch {
                print("Error: \(error)")
            }
        })
        downloadTask.resume()

【问题讨论】:

标签: ios swift avfoundation avplayerviewcontroller


【解决方案1】:

您应该使用 fileURLWithPath 从应用文件运行视频,如下所示:

func PlayVideo() {

    let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

    let player = AVPlayer(url: URL(fileURLWithPath: docPath).appendingPathComponent("IMG_3039.mp4"))

    let controller = AVPlayerViewController()
    controller.player = player

    self.present(controller, animated: true) {
        player.play()
    }
}

【讨论】:

【解决方案2】:

您在播放下载的视频时使用了错误的 URL。请仔细检查您的代码,并注意您使用的是播放器的文档目录 URL,而不是实际的视频。

【讨论】:

  • 我不是,参见.appendingPathComponent(video.uuid)
  • 对不起。您是否在移动后但在播放之前检查了该 URL 上的视频文件是否存在?
  • 是的,在使用模拟器时,我什至可以在我的 Mac 上使用 Finder 找到文件并使用 QuickTime 播放器播放。
  • 您是否尝试过使用 KVO 观察播放器的“状态”属性?然后在状态发生变化时打印播放器的“错误”属性?这可能会为您提供有关它为什么不播放的更多信息。
猜你喜欢
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多