如何使用 URL 的数组在 AVPlayer 中播放本地视频?

How to play local video in AVPlayer using an array of URL's?

我正在开发一个 iOS 应用程序,我希望能够在您从 table 视图 select 一行时播放本地视频文件。在一个单元格行被 selected 之后,它会转到一个 AVPlayerViewController 应该播放视频的地方,但是在执行了 segue 并且 AVPlayerViewController 出现后,它包含的 AVPlayer 仍然是空白的。所有控件均显示,但播放器呈现空白。玩家不玩游戏有什么原因吗?我已将我所有的视频添加到一个文件夹中,并将该视频文件夹添加到我的主包中,所以我不确定为什么播放器不播放这些视频。任何关于如何让玩家玩耍和工作的建议都将不胜感激。我还在故事板中触发了 AVPlayerViewController 的转场,每次点击单元格时它都会转场。

视频加载代码:

 import UIKit
 import AVKit
 import AVFoundation

  class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var playerViewController = AVPlayerViewController()
var player = AVPlayer()
var videoURL = [URL]()
var videoUrl = [URL]()
var drillVid = URL(fileURLWithPath: String())

override func viewDidLoad() {
    super.viewDidLoad()
videoUrl = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBBw:Pound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBtwLegwPound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallCrosswPound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallHiLo.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallThruHoop.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallIOw:Wiper.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallJuggle.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallInOut.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallWiper.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwpBallOverDribble.mp4")]


    tableView.delegate = self
    tableView.dataSource = self }

播放器渲染代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

drillVid = videoUrl[indexPath.row]

    return cell
}

显示 AVPlayerViewController 的代码,它的播放器应该播放视频,但是没有播放它只是空的:

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  drillVid = videoUrl[indexPath.row]. }


  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


        if segue.identifier == "playDrill" {
                let destination = segue.destination as! AVPlayerViewController
            if let indexPath = self.tableView.indexPathForSelectedRow {
                    drillVid = videoURL[indexPath.row]
                    let destination = segue.destination as! AVPlayerViewController
                    destination.player = AVPlayer(url: drillVid)
                    destination.player?.play()

                }
                }

[URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBBw:Pound.mp4")

这不是您在捆绑包中获取文件的方式..

let path = Bundle.main.path(forResource: "Pound", ofType: "mp4")

示例:将 "video" 添加到您的 .mp4 格式的项目中。 Select 视频并在右侧面板中,将目标设置为您的应用。这会将视频添加到您的 Main-Bundle.

在故事板中,添加一个按钮。添加一个 AVPlayerController。 Control + 单击该按钮并将其拖动到 AVPlayerController。

在视图控制器代码中,覆盖函数 prepareForSegue。获取其目的地并将其设置为 player 以通过抓取视频路径来播放视频..

代码如下:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination as! AVPlayerViewController

        let url = URL(fileURLWithPath: Bundle.main.path(forResource: "Video", ofType: "mp4")!)
        destination.player = AVPlayer(playerItem: AVPlayerItem(url: url))
        destination.player?.play()
    }

}

当您 运行 代码并按下按钮时,它会播放视频。我自己在 iPhone 6S、iOS 11.

上进行了测试