【发布时间】: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