I'm using AVPlayer
for displaying video in my TableViewCell
. It is playing in TableViewCell
, but the problem is that I'm not able to stop it.
I've used this Code to play video in cellforRow
avPlayer = AVPlayer(url: videoURL! as URL)
playerLayer = AVPlayerLayer(player: avPlayer)
playerLayer.frame = CGRect(x: cell.videoview.frame.origin.x, y: cell.videoview.frame.origin.x, width:cell.videoview.frame.size.width, height: cell.videoview.frame.size.height)
cell.videoview.layer.addSublayer(playerLayer)
avPlayer?.play()
The avPlayer
and playerLayer
are defined in the viewController
like this
var avPlayer : AVPlayer?
var playerLayer = AVPlayerLayer()
I'm using this code to stop the video
self.avPlayer?.pause()
self.playerLayer.removeFromSuperlayer()
self.avPlayer?.rate = 0.0
self.avPlayer?.replaceCurrentItem(with: nil)
self.avPlayer = nil
My question is this how can I stop the video running through AVPlayer
on AVPlayerLayer
?
Please suggest a solution, if there is any alternative for this please suggest that as well.