I have been trying to add some videos into my tableView using the AVPlayer which I placed into the TableView Cell.
But the UI freezes while the video is being loaded.
This is my cellforRow at indexpath.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCel {
var tableCell : UITableViewCell? = nil
if (tableCell == nil){
tableCell = tableView.dequeueReusableCell(withIdentifier: "cell")
}
let view = tableCell?.viewWithTag(9999)
tableCell?.tag = indexPath.row
DispatchQueue.main.async {
if (tableCell?.tag == indexPath.row){
let player = self.cache.object(forKey: indexPath.row as AnyObject) as! AVPlayer
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = (view?.bounds)!
view?.layer.addSublayer(playerLayer)
player.play()
}
// tableView.reloadData()
}
return tableCell!
}
This is how I added the videos to the cache.
for i in 0...10{
var videoURL : URL? = nil
if (i%2 == 0){
videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
}else{
videoURL = URL(string: "http://techslides.com/demos/sample-videos/small.mp4")
}
let player = AVPlayer(url: videoURL!)
arr?.append(player)
self.cache.setObject(player, forKey: i as AnyObject)
print("count = \(arr?.count)")
}
What would be the best method to have this resolved?