mirror of
https://github.com/metabolist/metatext.git
synced 2024-12-18 11:46:27 +00:00
29 lines
610 B
Swift
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
|
|
}
|
|
}
|