在活跃的UITableView的单元格中快速播放video

目标: – 我试图在完全可见的细胞中播放video(可能是2,3或1),它应该停止与cell不可见性

我用AVPlayer使用UITableview 。 我的桌面充满了video上市目前我正在使用捆绑video,
我使用下面的代码,但它播放不正确的&weired序列。 我在这里做错了什么? 用另一个逻辑来回答对我来说也很好。

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() } } 

有些链接不能帮助我

StackOverflow post1

StackOverflow post2

StackOverflow post3

这里是提供您在Tableview和UICollectionView中播放video的链接。 你可以使用这个框架在tableview中播放video。 MMPlayerView帮助您在单元格完全可见时自动播放video,并在单元格消失时停止播放video。 我也使用这个播放器在collections视图播放video,它的作品像魅力。

https://github.com/MillmanY/MMPlayerView