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

标签 ios swift video uicollectionview inline

UICollectionView 将包含视频的提要。当用户观看视频时,我希望它能播放。使用我当前的设置,一旦将多个视频加载到 Collection View 中,它们就会同时播放(我想这取决于分页)。

How do I play videos inline in a 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
        }
    }
}

FeedViewController 中 Feed 的 collectionView cellForItemAt indexPath 委托(delegate)方法如下:

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
  }

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

class MyCollectionViewCell {

    @IBOutlet weak var playerView: PlayerViewClass!


override func awakeFromNib() {
    super.awakeFromNib() 

   //Setup, not relevant

   }

}

我找到了以下 GitHub repo ,它显示了我想要实现的功能;但是,我有点不确定如何使用下面的设置执行此操作。

非常感谢!

最佳答案

您需要知道哪些单元格是可见的。您可以通过 UICollectionView API 的 visibleCells 属性“免费”获得它。这是 documentation .

此外,您还需要在 UICollectionView 滚动时进行一些簿记。在查看 UICollectionView 的文档时,您会发现它是 UIScrollView 的子类。 UIScrollView 带有委托(delegate)方法,使您能够跟踪它。这里有一个“诗人的物理学”方法,可以帮助您完成这项任务:

假设这是您的 View Controller :

class YourViewController: UIViewController {
    let collectionView = UICollectionView()

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
    }
}

然后,您将在此处实现您的 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()
    }
}

最后,您将实现 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
        }
    }
}

您会想弄清楚停止/启动动画的时间,看看什么看起来不错,所以请随意浏览各种 UIScrollViewDelegate 方法。

关于ios - 如何在 UICollectionView Swift 中内联播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566288/

相关文章:

ios - 在 Objective C 中,如何在不保存为本地文件的情况下播放 NSData 中的视频文件?

c# - 以特定时间间隔从视频(mpg、wmv 等)中捕获帧

ios - 如何根据标签大小调整 CollectionView 高度

ios - GLDrawElements 崩溃程序

ios - CollectionView-将pdf文件拖放到collectionView上(loadObject(ofClass :)无法正常工作)

ios - 如何以编程方式返回具有全局功能的 Root View Controller ?

javascript - HTML5 视频在服务器上托管后无法在 chrome、IE 上运行,视频为 mov 文件(quicktime 格式)

ios - 比较 2 个贝塞尔路径?

ios - 未使用的场景从 Storyboard 中临时隐藏

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?