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)
}}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...