Fix autoplay

This commit is contained in:
Justin Mazzocchi 2021-02-05 22:54:26 -08:00
parent ec20a3072f
commit 9c97026881
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 17 additions and 12 deletions

View file

@ -3,21 +3,23 @@
import AVKit import AVKit
final class PlayerCache { final class PlayerCache {
private let cache = NSCache<NSURL, AVQueuePlayer>() private let cache = NSCache<NSURL, AVPlayer>()
private var allURLsCached = Set<URL>() private var allURLsCached = Set<URL>()
private init() {} private init() {
cache.countLimit = 4
}
} }
extension PlayerCache { extension PlayerCache {
static let shared = PlayerCache() static let shared = PlayerCache()
func player(url: URL) -> AVQueuePlayer { func player(url: URL) -> AVPlayer {
if let player = cache.object(forKey: url as NSURL) { if let player = cache.object(forKey: url as NSURL) {
return player return player
} }
let player = AVQueuePlayer(url: url) let player = AVPlayer(url: url)
cache.setObject(player, forKey: url as NSURL) cache.setObject(player, forKey: url as NSURL)
allURLsCached.insert(url) allURLsCached.insert(url)

View file

@ -1,6 +1,7 @@
// Copyright © 2020 Metabolist. All rights reserved. // Copyright © 2020 Metabolist. All rights reserved.
import AVKit import AVKit
import Combine
import Kingfisher import Kingfisher
import UIKit import UIKit
import ViewModels import ViewModels
@ -28,7 +29,7 @@ final class AttachmentView: UIView {
private let viewModel: AttachmentViewModel private let viewModel: AttachmentViewModel
private let parentViewModel: AttachmentsRenderingViewModel private let parentViewModel: AttachmentsRenderingViewModel
private var playerLooper: AVPlayerLooper? private var playerCancellable: AnyCancellable?
init(viewModel: AttachmentViewModel, parentViewModel: AttachmentsRenderingViewModel) { init(viewModel: AttachmentViewModel, parentViewModel: AttachmentsRenderingViewModel) {
self.viewModel = viewModel self.viewModel = viewModel
@ -64,11 +65,15 @@ extension AttachmentView {
func play() { func play() {
let player = PlayerCache.shared.player(url: viewModel.attachment.url) let player = PlayerCache.shared.player(url: viewModel.attachment.url)
if let cachedPlayerLooper = Self.playerLooperCache[player] { playerCancellable = NotificationCenter.default.publisher(
playerLooper = cachedPlayerLooper for: .AVPlayerItemDidPlayToEndTime,
} else if let item = player.currentItem { object: player.currentItem)
playerLooper = AVPlayerLooper(player: player, templateItem: item) .sink { _ in
Self.playerLooperCache[player] = playerLooper player.currentItem?.seek(to: .zero) { success in
guard success else { return }
player.play()
}
} }
player.isMuted = true player.isMuted = true
@ -94,8 +99,6 @@ extension AttachmentView {
} }
private extension AttachmentView { private extension AttachmentView {
static var playerLooperCache = [AVQueuePlayer: AVPlayerLooper]()
// swiftlint:disable:next function_body_length // swiftlint:disable:next function_body_length
func initialSetup() { func initialSetup() {
addSubview(imageView) addSubview(imageView)