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

标签 ios url video avplayer avplayerviewcontroller

我正在开发一个 iOS 应用程序,当您从表格 View 中选择一行时,我希望能够播放本地视频文件。选择单元格行后,它会转到应播放视频的 AVPlayerViewController ,但是在执行 segue 并呈现 AVPlayerViewController 后,它包含的 AVPlayer 保持空白。所有控件都显示,但播放器呈现空白。玩家不玩是有原因的吗?我已将所有视频添加到一个文件夹中,并将该视频文件夹添加到我的主包中,所以我不确定播放器为什么不播放视频。任何关于我如何让玩家玩耍和工作的建议都将不胜感激。我还触发了 Storyboard 中 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")
示例:使用 .mp4 格式将“视频”添加到您的项目中。选择视频,然后在右侧面板中,将目标设为您的应用。这会将视频添加到您的 Main-Bundle。

在您的 Storyboard中,添加一个按钮。添加一个 AVPlayerController。 Control + 单击该按钮并将其拖动到 AVPlayerController。

enter image description here

在 View Controller 代码中,重写函数 prepareForSegue。获取其目的地并将其设置为player通过抓取视频的路径来播放视频..

enter image description here
enter image description here
enter image description here

代码如下所示:

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 上进行了测试。

关于ios - 如何使用 URL 数组在 AVPlayer 中播放本地视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285555/

相关文章:

ios - 协议(protocol)和委托(delegate)不起作用..我做错了什么?

html - CSS 动画并不总是在 iOS 8 Safari 中启动

ios - 查找 subview 中的所有 UIButton

perl - 如何从 Perl 中的 URL 中仅提取方案、主机和端口?

javascript - js MediaRecorder + ffmpeg

HTML5 视频标签不起作用

ios - NSMutableArray 未被填充

python-3.x - 最简单的 python 链接缩短器

php - 在 URL 中显示文件扩展名的优点?

c++ - 如何使用 avformat_open_input 函数 (ffmpeg)