1
0
Fork 0
mirror of https://github.com/metabolist/metatext.git synced 2024-12-29 08:50:27 +00:00
metatext/Caches/PlayerCache.swift
Justin Mazzocchi 9c97026881
Fix autoplay
2021-02-05 23:02:44 -08:00

29 lines
610 B
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import AVKit
final class PlayerCache {
private let cache = NSCache<NSURL, AVPlayer>()
private var allURLsCached = Set<URL>()
private init() {
cache.countLimit = 4
}
}
extension PlayerCache {
static let shared = PlayerCache()
func player(url: URL) -> AVPlayer {
if let player = cache.object(forKey: url as NSURL) {
return player
}
let player = AVPlayer(url: url)
cache.setObject(player, forKey: url as NSURL)
allURLsCached.insert(url)
return player
}
}