如何在 UICollectionView Swift 中内联播放视频?

Ser*_*les 5 video inline ios uicollectionview swift

UICollectionView 将包含视频提要。当用户内联视频时,我希望它播放。使用我当前的设置,一旦加载到集合视图中,就会同时播放几个视频(我想取决于分页)。

如何在 UICollectionView 中内嵌播放视频?

UICollectionView 提要中的一个单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的类PlayerViewClass

import Foundation
import UIKit
import AVKit
import AVFoundation

class PlayerViewClass: UIView {

    override static var layerClass: AnyClass {
        return AVPlayerLayer.self
    }

    var playerLayer: AVPlayerLayer {
    
        return layer as! AVPlayerLayer
    }

    var player: AVPlayer? {
        get {
            return playerLayer.player
        }
    
        set {
            playerLayer.player = newValue
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在饲料的的CollectionView cellForItemAt indexPath委托方法FeedViewController如下:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        
      if let cell = cell as? MyCollectionViewCell {

      ...

      //Configuring the cell

      ...
    
      //Video player
      let avPlayer = AVPlayer(url: post.fullURL)
        
      //Setting cell's player
      cell.playerView.playerLayer.player = avPlayer
  
      //TODO: Change so this is only executed when the user is inline.
      cell.playerView.player?.play()
    
    }
    return cell
  }
Run Code Online (Sandbox Code Playgroud)

单元格MyCollectionViewCell有一个链接到 playerView UIView 的 IBOutlet:

class MyCollectionViewCell {

    @IBOutlet weak var playerView: PlayerViewClass!


override func awakeFromNib() {
    super.awakeFromNib() 

   //Setup, not relevant

   }

}
Run Code Online (Sandbox Code Playgroud)

我找到了以下GitHub repo,它显示了我想要实现的功能;但是,我有点不确定如何使用下面的设置来做到这一点。

非常感谢!

Adr*_*ian 2

UICollectionView您需要知道哪些单元格可见。您可以通过API 的属性“免费”获得该信息visibleCells。这是文档

此外,当卷轴滚动时,您需要做一些簿记UICollectionView。在查看 的文档时UICollectionView,您会发现它是 的子类UIScrollViewUIScrollView带有委托方法,使您能够跟踪这一点。以下是“诗人的物理学”方法,可以帮助您完成此任务:

假设这是您的视图控制器:

class YourViewController: UIViewController {
    let collectionView = UICollectionView()

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在这里实现您的UICollectionViewDelegateUICollectionViewDataSource方法:

extension YourViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    // Bare bones implementation
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // TODO: need to implement
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // TODO: need to implem,ent
        return UICollectionViewCell()
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,您将实现UIScrollViewDelegate检测滚动开始和滚动结束的方法。您可以在此处实现启动/停止视频的逻辑:

extension YourViewController: UIScrollViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        collectionView.visibleCells.forEach { cell in
            // TODO: write logic to stop the video before it begins scrolling
        }
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        collectionView.visibleCells.forEach { cell in
            // TODO: write logic to start the video after it ends scrolling
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能想要调整停止/开始动画的时间,看看什么看起来不错,所以请随意探索各种UIScrollViewDelegate方法。