【问题标题】:How do I play videos inline in a UICollectionView Swift?如何在 UICollectionView Swift 中内联播放视频?
【发布时间】:2019-12-25 05:41:17
【问题描述】:

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

如何在 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委托方法如下:

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,它显示了我想要实现的功能;但是,我有点不确定如何使用下面的设置来执行此操作。

非常感谢!

【问题讨论】:

    标签: ios swift video uicollectionview inline


    【解决方案1】:

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

    此外,当UICollectionView 滚动时,您需要进行一些记账。在查看UICollectionView 的文档时,您会发现它是UIScrollView 的子类。 UIScrollView 带有委托方法,使您能够跟踪它。下面是一个“诗人物理学”方法,你可以做些什么来完成这项任务:

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

    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 方法。

    【讨论】:

    • 嗨,阿德里安,非常感谢!这无济于事!我有一个问题:有没有办法确定用户当前视图中(或最接近视图中心)的视频单元格,以便我可以实现 scrollViewDidEndDecelerating 和 scrollViewWillBeginDragging 方法?
    • 环顾四周,这似乎回答了这个问题:stackoverflow.com/questions/34899689/…
    • 同意。但是,(我忘了提),在我当前的布局约束下,视图容器在任何给定时间最多有两个单元格。
    • 您希望将可见单元格的中心与 UICollectionView 的中心进行比较,并确定哪个单元格最接近。您需要比较 CGPoints。
    【解决方案2】:

    要在 Inline 中播放视频,您可以参考 gitHub 中的 MMPlayerView,这将是一个有用的框架“https://cocoapods.org/pods/MMPlayerView

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2017-10-08
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多