我有一个UITableView
,用于渲染包含gif的推文。这些都是从Twitter作为MP4返回的,所以我正在使用AVPlayer
。我使用HanekeSwift
缓存项目,然后在表视图中播放它。我遇到的问题是加载tableview时屏幕上的任何推文都不会自动播放,我必须在屏幕上滚动这些推文然后重新打开。
我通过以下功能配置AVPlayer
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()
}
}
遵循与here类似的内容
但是,对我而言,该单元格基本上已暂停,直到我滚动为止。