ios - AsyncDisplayKit 问题在 Swift 中同时播放多个视频
问题描述
我正在使用纹理(AsyncDisplayKit)。在其中,我创建了 ASTableNode 并添加了分页,我想创建像 tiktok 这样的视频源。我的代码正在运行,但同时播放了两个视频。我希望一个视频一次全屏播放,与 Tiktok 中的视频提要相同
我正在关注https://mux.com/blog/building-tiktok-smooth-scrolling-on-ios/教程。
这是我的代码
class ViewController: UIViewController {
var currentPage: Int = 1
var arrayVideos = [String]()
var tableNode: ASTableNode = ASTableNode(style: .plain)
override func viewDidLoad() {
super.viewDidLoad()
self.applyStyle()
self.tableNode.leadingScreensForBatching = 3.0
self.view.addSubnode(tableNode)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.tableNode.frame = self.view.bounds
}
func applyStyle() {
self.tableNode.delegate = self
self.tableNode.dataSource = self
self.tableNode.view.separatorStyle = .singleLine
self.tableNode.view.allowsSelection = true
self.tableNode.isPagingEnabled = true
}}
extension ViewController: ASTableDataSource, ASTableDelegate {
func tableNode(_ tableNode: ASTableNode, numberOfRowsInSection section: Int) -> Int {
return arrayVideos.count
}
func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock {
let cellNodeBlock:() -> ASCellNode = {
let post = self.arrayVideos[indexPath.row]
let cell = VideoPlayerTableCell(with: post)
return cell
}
return cellNodeBlock
}
func tableNode(_ tableNode: ASTableNode, constrainedSizeForRowAt indexPath: IndexPath) -> ASSizeRange {
let width = UIScreen.main.bounds.size.width
let min = CGSize(width: width, height: (UIScreen.main.bounds.size.height))
let max = CGSize(width: width, height: .infinity)
return ASSizeRangeMake(min, max)
}
func shouldBatchFetchForTableNode(tableNode: ASTableNode) -> Bool {
return true
}
func tableNode(_ tableNode: ASTableNode, willBeginBatchFetchWith context: ASBatchContext) {
//Fetching video from server
self.getVideos { newPosts in
self.currentPage = self.currentPage + 1
self.insertNewRowsInTableNode(newPosts: newPosts)
context.completeBatchFetching(true)
}
}
func insertNewRowsInTableNode(newPosts: [String]) {
guard !newPosts.isEmpty else {
return
}
let section = 0
var indexPaths: [IndexPath] = []
let total = arrayVideos.count + newPosts.count
for row in arrayVideos.count ... total - 1 {
let path = IndexPath(row: row, section: section)
indexPaths.append(path)
}
arrayVideos.append(contentsOf: newPosts)
tableNode.insertRows(at: indexPaths, with: .none)
}}
class VideoPlayerTableCell: ASCellNode {
var videoNode: ASVideoNode
var post: String
init(with post: String) {
self.post = post
self.videoNode = ASVideoNode()
super.init()
self.videoNode.shouldAutoplay = true
self.videoNode.shouldAutorepeat = true
self.videoNode.gravity = AVLayerVideoGravity.resizeAspectFill.rawValue
DispatchQueue.main.async() {
self.videoNode.asset = AVAsset(url: URL(string: self.post)!)
}
self.addSubnode(self.videoNode)
}}
解决方案
推荐阅读
- php - 如何在 .tpl 文件中包含 php 代码
- python - Dockerized web-service does not run in background even with detached flag
- sql-server-2008 - SQL: Select count and percentage with conditions
- ruby-on-rails - Update model field with button Rails
- java - 为二叉树创建路径 Arraylist 的 ArrayList?
- java - Spring security custom UserDetails not authenticating
- mysql - 带有用户/密码的 mysql --defaults-extra-file 产生“访问被拒绝...”
- amazon-web-services - What is exactly "Assume" a role in AWS?
- bash - 除法导致 '0' 在 bash 中,但分子和分母的值应该导致不同的结果
- python-3.x - Anaconda自带的python Vscode无法进入调试模式