【问题标题】:How to play one video at a time in a collectionView?如何在 collectionView 中一次播放一个视频?
【发布时间】:2019-08-28 17:18:15
【问题描述】:

我正在构建一个类似于 TikTok 的应用程序,其中主要提要是一个 UICollectionView,每个单元格中都有一个视频。我的问题是多个视频将同时播放,我只希望一次播放一个视频,具体取决于单元格是否在视图中。如果单元格不在视野范围内,我还希望视频暂停。

每个单元格占据整个屏幕,并且 collectionView 启用了分页。

如果您曾经使用过 Instagram 或 TikTok,我希望功能相同。视频仅在单元格进入视图时播放,视频在单元格离开视图时暂停。如果您需要更多代码或信息,请告诉我,谢谢!

这里是FeedController中的相关代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! FeedCell
    cell.delegate = self

    cell.post = posts[indexPath.row]

    if let videoUrl = posts[indexPath.row].videoUrl { 
      player = AVPlayer(url: videoUrl)
      playerLayer = AVPlayerLayer(player: player)

      playerLayer?.zPosition = -1
      playerLayer?.frame = cell.contentView.frame
      playerLayer?.videoGravity = .resizeAspectFill
      cell.contentView.layer.addSublayer(playerLayer!)
      player?.play()

      cell.playerLayer = playerLayer
      cell.player = player
    }

    return cell
}

FeedCell的相关代码如下:

class FeedCell: UICollectionViewCell {

  var post: Post?

  var playerLayer: AVPlayerLayer?
  var player: AVPlayer?

  override func prepareForReuse() {
      super.prepareForReuse()
      playerLayer?.removeFromSuperlayer()
      player?.pause()
  }

}

【问题讨论】:

    标签: ios swift collectionview


    【解决方案1】:

    您可以使用collectionView(:willDisplay:forItemAt:)collectionView(:didEndDisplaying:forItemAt:) 委托方法轻松实现此目的。

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        (cell as? FeedCell).player?.play()
    }
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        (cell as? FeedCell).player?.pause()
    }
    

    注意:只有在您的单元格完全离开屏幕后才会调用 didEndDisplaying。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多