AVPlayer not starting until cell is recycled

100 Views Asked by At

I have a UITableView that I am using to render out tweets that contain gifs. These are returned from Twitter as MP4's so I am using AVPlayer. Using HanekeSwift I am caching the item and then playing it in the tableview. The issue I am having is that any Tweets on screen when the tableview load, do not auto play, I must scroll them off screen and back on before they play.

I configure my AVPlayer in the following function

private func configureAVPlayer(url: URL) {
    cache.fetch(URL: url).onSuccess { [weak self] stream in
        guard let path = URL(string: DiskCache.basePath())?.appendingPathComponent("shared-data/original") else { return }

        let cached = DiskCache(path: path.absoluteString).path(forKey: url.absoluteString)
        let file = URL(fileURLWithPath: cached)

        if !FileManager.default.fileExists(atPath: cached) {
            try! stream.write(to: file, options: .atomicWrite)
        }

        self?.player = AVPlayer(url: file)
        self?.player?.automaticallyWaitsToMinimizeStalling = false
        self?.playerLayer = self?.avPlayerView.layer as? AVPlayerLayer
        guard let player = self?.player else { return }
        self?.playerLayer?.player = player
        self?.player?.play()
    }
}

Following something very similar as described here

However for me, the cell is essentially paused until I scroll.

0

There are 0 best solutions below