如何在 UICollectionView Swift 中内联播放视频?
How do I play videos inline in a UICollectionView Swift?
一个 UICollectionView 将包含一个 feed 视频。当用户观看视频时,我希望它能播放。使用我当前的设置,一旦将多个视频加载到集合视图中,它们就会同时播放(我想这取决于分页)。
How do I play videos inline in a UICollectionView?
UICollectionView 提要中的一个单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的 class 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
}
}
}
Feed在FeedViewController中的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 有一个 IBOutlet 链接到 playerView UIView:
class MyCollectionViewCell {
@IBOutlet weak var playerView: PlayerViewClass!
override func awakeFromNib() {
super.awakeFromNib()
//Setup, not relevant
}
}
我找到了以下 GitHub repo,它显示了我想要实现的功能;但是,我有点不确定如何使用下面的设置执行此操作。
非常感谢!
您需要知道哪些单元格是 visible.You 通过 UICollectionView
API 的 visibleCells
属性 获取 "for free" .这是 documentation.
此外,当 UICollectionView
滚动时,您需要做一些簿记。在查看 UICollectionView
的文档时,您会发现它是 UIScrollView
的子类。 UIScrollView
带有委托方法,使您能够跟踪它。这是完成此任务的 "physics for poets" 方法:
假设这是您的视图控制器:
class YourViewController: UIViewController {
let collectionView = UICollectionView()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
}
然后,您将在此处实施 UICollectionViewDelegate
和 UICollectionViewDataSource
方法:
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
方法来检测滚动的开始和结束。这是您为 starting/stopping 视频实现逻辑的地方:
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
}
}
}
你会想弄乱 stopping/starting 动画的时间,看看什么看起来不错,所以请随意浏览各种 UIScrollViewDelegate
方法。
要在 Inline 中播放视频,您可以参考 gitHub 中的 MMPlayerView,这将是一个有用的框架“https://cocoapods.org/pods/MMPlayerView”
一个 UICollectionView 将包含一个 feed 视频。当用户观看视频时,我希望它能播放。使用我当前的设置,一旦将多个视频加载到集合视图中,它们就会同时播放(我想这取决于分页)。
How do I play videos inline in a UICollectionView?
UICollectionView 提要中的一个单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的 class 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
}
}
}
Feed在FeedViewController中的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 有一个 IBOutlet 链接到 playerView UIView:
class MyCollectionViewCell {
@IBOutlet weak var playerView: PlayerViewClass!
override func awakeFromNib() {
super.awakeFromNib()
//Setup, not relevant
}
}
我找到了以下 GitHub repo,它显示了我想要实现的功能;但是,我有点不确定如何使用下面的设置执行此操作。
非常感谢!
您需要知道哪些单元格是 visible.You 通过 UICollectionView
API 的 visibleCells
属性 获取 "for free" .这是 documentation.
此外,当 UICollectionView
滚动时,您需要做一些簿记。在查看 UICollectionView
的文档时,您会发现它是 UIScrollView
的子类。 UIScrollView
带有委托方法,使您能够跟踪它。这是完成此任务的 "physics for poets" 方法:
假设这是您的视图控制器:
class YourViewController: UIViewController {
let collectionView = UICollectionView()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
}
然后,您将在此处实施 UICollectionViewDelegate
和 UICollectionViewDataSource
方法:
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
方法来检测滚动的开始和结束。这是您为 starting/stopping 视频实现逻辑的地方:
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
}
}
}
你会想弄乱 stopping/starting 动画的时间,看看什么看起来不错,所以请随意浏览各种 UIScrollViewDelegate
方法。
要在 Inline 中播放视频,您可以参考 gitHub 中的 MMPlayerView,这将是一个有用的框架“https://cocoapods.org/pods/MMPlayerView”