Target :- I am trying to play video in fully visible cell (it may be 2,3 or 1) & it should be stop with invisibility of cell
I am using UITableview
with AVPlayer
. My tableview is full of video listing currently i am using videos from bundle,
I used below code, But it's playing incorrectly & weired sequence.
What i am doing wrong here?
Answer with another logic is also fine for me.
ViewController.swift -
protocol VideoActivityDelegate {
func startVideo()
func stopVideo()
}
// MARK: TableView datasource delegate
var videoDelegate: VideoActivityDelegate?
extension ViewController :UITableViewDataSource,UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! VideoTableViewCell
videoDelegate = cell
cell.selectionStyle = .none
cell.videoPlayerItem = AVPlayerItem.init(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video\(indexPath.row+1)", ofType: ".mp4")!))
return cell
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
//print("scrollViewWillBeginDragging")
for indexes in tableViewHome.indexPathsForVisibleRows! {
let cellRect = tableViewHome.rectForRow(at: indexes)
if tableViewHome.bounds.contains(cellRect) {
}
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("displaing ---> \(indexPath.row)")
videoDelegate?.startVideo()
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("Ending \(indexPath.row)")
//videoDelegate?.stopVideo()
}
}
VideoTableViewCell.Swift
class VideoTableViewCell: UITableViewCell {
@IBOutlet weak var videoView: UIView!
var avPlayer: AVPlayer?
var avPlayerLayer: AVPlayerLayer?
var videoPlayerItem: AVPlayerItem? = nil {
didSet {
avPlayer?.replaceCurrentItem(with: self.videoPlayerItem)
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.setupMoviePlayer()
}
func setupMoviePlayer(){
self.avPlayer = AVPlayer.init(playerItem: self.videoPlayerItem)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
avPlayer?.volume = 3
avPlayer?.actionAtItemEnd = .none
avPlayerLayer?.frame = videoView.bounds
//self.backgroundColor = .clear
self.videoView.layer.insertSublayer(avPlayerLayer!, at: 0)
NotificationCenter.default.addObserver(self,
selector: #selector(self.playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer?.currentItem)
}
@objc func playerItemDidReachEnd(notification: Notification) {
let p: AVPlayerItem = notification.object as! AVPlayerItem
p.seek(to: kCMTimeZero, completionHandler: nil)
}
}
extension VideoTableViewCell :VideoActivityDelegate {
func startVideo(){
self.avPlayer?.play()
}
func stopVideo() {
self.avPlayer?.pause()
}
}
Some link does not helps me