Ich entwickle eine App für iOS und ich habe Code erstellt, wo, sobald eine Zelle in einer Tabellenansicht ausgewählt ist, es zu einem AVPlayerViewController wechselt und einen lokalen Videoclip abspielt, abhängig davon, welche Zellenzeile ausgewählt wurde. Wenn es jedoch zum AVPlayerViewController wechselt, erscheint der Player, aber es wird kein Video abgespielt. Warum? Ich habe der didSelectAtRow
-Funktion sogar eine Druckanweisung hinzugefügt, um sicherzustellen, dass die richtige Videodatei ausgewählt wird. Es sollte beachtet werden, dass der Code gut funktioniert, bis es Zeit ist, eine Videodatei abzuspielen (der Player erscheint, bleibt aber leer).AVPlayerViewController spielt kein Video?
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()
}
}
}
}
}
Ich denke, Ihr Video Weg falsch ist. Jetzt befindet sich der Videodateipfad dort, wo sich das Video auf Ihrem Desktop befindet. Sie müssen die Videodateien zu Ihrem Projekt hinzufügen und dann müssen Sie auf diese Videodateien zugreifen. –
Ich habe die Dateien zu meinem Projekt hinzugefügt –