mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-21 15:50:59 +00:00
Fix autoplay
This commit is contained in:
parent
ec20a3072f
commit
9c97026881
2 changed files with 17 additions and 12 deletions
|
@ -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)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue