Currently I have my code setup to where in each table view cell there is a button that displays a video after is pressed. Each table view cell row contains the button (there
First you have to distinguish the buttons added in the cell by giving them tag value in tableView:cellForRowAtIndexPath: method
cell.button.tag = indexpath.row
This will set different tag values to the buttons present in each cell. Then add below to it:
cell.button.addTarget(self, action:(YourController.buttonMethodPlayVideo(:)) , forControlEvents: .TouchUpInside)
Create a method for button to perform action on click:
func buttonMethodPlayVideo(sender: UIButton) {
print(sender.tag)
}
In above method when you will click on button you will get different tag values. According to that value you can play different videos or pass different video name to player to play vudeo.
I believe you should refactor your code to get required behaviour. Please check the following code:
First make changes in VideoPlayerView
method named setupPlayerView
.Replace your implementation with this:
func setupPlayerView(for url: URL) {
player = AVPlayer(url: url)
//video only renders if you specify 'playerLayer'
let playerLayer = AVPlayerLayer(player: player)
self.layer.insertSublayer(playerLayer, at: 1)
playerLayer.frame = frame
player?.play()
//attached obeserver of 'player' to tell when 'player' is ready
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
}
Now make change in DrillsTableViewCell
,make changes in videosURLs and I added a new variable singleVideoURL, your new class will look like this:
class DrillsTableViewCell: UITableViewCell {
var videoURLs:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")]
@IBOutlet weak var logoImage: UIImageView!
@IBOutlet weak var drillTitle: UILabel!
@IBOutlet weak var playButton: UIButton!
@IBAction func watchButton(_ sender: UIButton) {
print(123)
//controls video background view
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
var singleVideoURL = videoURLs[sender.tag]
view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9 / 16)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
videoPlayerView .setupPlayerView(for: singleVideoURL)
view.addSubview(videoPlayerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//possible features implemented later
UIApplication.shared.isStatusBarHidden = true
})
}
Setup an array to put your videos' paths into, and its indexe should match the index of the cell. In addition, you can pass the row number of the cell to the button inside as its tag. So when you tap on that button, you can find a specific video path in the array through the button's tag.