I am using Texture (AsyncDisplayKit). In which, I have created ASTableNode and added pagination and I want to create video feed like tiktok. My code is working but two videos playing at a same time. I want one video should play at a time on full screen same as Video feed in Tiktok
I am following https://mux.com/blog/building-tiktok-smooth-scrolling-on-ios/ tutorial.
Here is my code
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)
}}