mirror of
https://github.com/metabolist/metatext.git
synced 2024-12-23 05:56:30 +00:00
28 lines
591 B
Swift
28 lines
591 B
Swift
|
// Copyright © 2020 Metabolist. All rights reserved.
|
||
|
|
||
|
import AVKit
|
||
|
|
||
|
final class PlayerCache {
|
||
|
private let cache = NSCache<NSURL, AVQueuePlayer>()
|
||
|
private var allURLsCached = Set<URL>()
|
||
|
|
||
|
private init() {}
|
||
|
}
|
||
|
|
||
|
extension PlayerCache {
|
||
|
static let shared = PlayerCache()
|
||
|
|
||
|
func player(url: URL) -> AVQueuePlayer {
|
||
|
if let player = cache.object(forKey: url as NSURL) {
|
||
|
return player
|
||
|
}
|
||
|
|
||
|
let player = AVQueuePlayer(url: url)
|
||
|
|
||
|
cache.setObject(player, forKey: url as NSURL)
|
||
|
allURLsCached.insert(url)
|
||
|
|
||
|
return player
|
||
|
}
|
||
|
}
|