swift - AVPlayerViewController 不播放视频?

标签 swift uitableview avplayer avplayerviewcontroller

我正在为 iOS 开发一个应用程序,我已经构建了代码,一旦选择了 TableView 中的单元格,它就会转至 AVPlayerViewController 并根据选择的单元格行播放本地视频剪辑。但是当它转到AVPlayerViewController时,播放器出现但没有播放视频,这是为什么?我什至在 didSelectAtRow 函数中添加了一条打印语句,以确保选择正确的视频文件。应该注意的是,在实际播放视频文件之前,代码工作得很好(播放器出现但仍为空)。

import UIKit
import AVKit
import AVFoundation

class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var initialRow = Int()
var arrayForKey2 = [[String]]()
var keyIndex = Int()
var headLabel = String()
var labels = Array(trainingDict.keys)
var playerViewController = AVPlayerViewController()
var player = AVPlayer()
var videoURL = [URL]()
var videoUrl = [URL]()
var drillVid = URL(fileURLWithPath: String())

var firstTips = ["Tip 1: Stay Hydrated", "Tip 1: Keep elbow tucked", "x", "Tip 1: Take quick breaks:", "Tip 1: Keep your head up", "Tip 1: Don't cross your feet", "Tip 1: Don't do more than 15 reps"]


@IBOutlet weak var tableView: DrillsTableView!

@IBOutlet weak var drillLabel: UILabel!

@IBOutlet weak var labelBackground: UIView!

@IBAction func back(_ sender: Any) {

    dismiss(animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    videoURL = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/LowDribble.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallBBw:Pound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallBBw:PickUp.mp4"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallBB.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallInOut.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallOverDribble.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallThruHoop.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallThruLeg.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallThruLegLo.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallWiper.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallIOBBCrossCombo.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallIOBetweenLegsCombo.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallComboFreestyle.mp4")]

    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
    drillLabel.text = labels[keyIndex]

    playerViewController.player = player



    for (key, value) in trainingDict {
        print("\(key) --> \(value)")
        objectArray.append(Objects(sectionName: key, sectionObjects: value))
    }
}




func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DrillsTableViewCell




    if initialRow == 1 {
    drillVid = videoURL[indexPath.row]

    }

    if initialRow == 2 {
        drillVid = videoUrl[indexPath.row]
    }

    print(initialRow)
    print(indexPath.row)
    print(drillVid)

}

//changes KeysController tip labels accordingly 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Tips" {
        if let keysVC = segue.destination as? KeysController {


            keysVC.keyTip1 = firstTips[initialRow]
        }
        if segue.identifier == "playDrill", initialRow == 1 {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                    drillVid = videoURL[indexPath.row]
                    let destination = segue.destination as! AVPlayerViewController
                    destination.player = playerViewController.player
                    playerViewController.player = player
                    player = AVPlayer(url: drillVid)
                    player.play()



                }

                if initialRow == 3 {
                    if let indexPath = self.tableView.indexPathForSelectedRow {
                    let video = videoUrl[indexPath.row]
                    let destination = segue.destination as! AVPlayerViewController
                    destination.player = AVPlayer(url: video)
                    destination.player?.play()
                }
            }
        }


    }
}

最佳答案

如果您已经将这些文件添加到项目中,那么您必须像下面这样访问这些文件:

videoURL = [URL(fileURLWithPath: Bundle.main.path(forResource: "LowDribble", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallBBw:Pound", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallBBw:PickUp", ofType: "mp4")!), URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallBB", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallInOut", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallOverDribble", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallThruHoop", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallThruLeg", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallThruLegLo", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallWiper", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallIOBBCrossCombo", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallIOBetweenLegsCombo", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallComboFreestyle", ofType: "mp4")!)]

videoUrl = [URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallBBw:Pound", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallBtwLegwPound", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallCrosswPound", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallHiLo", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "OneBallThruHoop", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallIOw:Wiper", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallJuggle", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallInOut", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwoBallWiper", ofType: "mp4")!),URL(fileURLWithPath: Bundle.main.path(forResource: "TwpBallOverDribble", ofType: "mp4")!)]

关于swift - AVPlayerViewController 不播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258323/

相关文章:

ios - 使用 IBOutlets 加载 NIB/XIB 文件

ios - 按该数组中的日期对 Swift 4 中的 Tableviews 部分进行排序

ios - 为什么 AVPlayer 在 'playbackBufferEmpty' 之后立即以 'playbackLikelyToKeepUp' 状态失败?

ios - watchOS 2 在 Swift 中的可达性

swift - 如何将 Swift 连接到托管在 Google Cloud 上的 postgresql 数据库

objective-c - 在iOS7中,为什么选中的单元格的背景色总是灰色?

uitableview - 单点触控 : UITableViewCell height

ios - 销毁 Controller 后恢复 avplayer

iOS:AVPlayerViewController View 未删除

ios - CLGeocoder CompletionHandler