So my app contains a uitableview/uitableviewcell
and inside the uitableview/uitableviewcell
is an AVplayer
. I want to be able to pause the video player when the button is tapped. When I made the AV player a global variable, I was able to pause and play the video, but the video wasn't being replaced by the next video when I scrolled.
// video player
let videoURL = URL(string: uVideoUrl)!
let player = AVPlayer(url: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x: cell.backgroundImg.frame.origin.x, y: cell.backgroundImg.frame.origin.y, width: cell.backgroundImg.frame.width - 5, height: cell.backgroundImg.frame.height)
cell.layer.addSublayer(playerLayer)
// play button
let playButton = UIButton(frame: CGRect(x: 15, y: 75, width: 30, height: 30))
playButton.setImage(UIImage(named: "playimg.png"), for: .normal)
** playButton.addTarget(self, action: #selector(playVideo(player:player)), for: .touchUpInside) **
cell.addSubview(playButton)
Here's the function to play the video.
func playVideo(player:AVPlayer) {
player.play()
}
The text/code with ** is the code I can't figure out how to let me pass in the player for me to play the video.