From aef8617eaca848bc37fbe4f0fe8e3bef4a1dca36 Mon Sep 17 00:00:00 2001 From: Vitor Ferraz Varela Date: Wed, 17 Mar 2021 18:19:41 -0300 Subject: [PATCH] fix: adjust observe AVItem status to call delegate only once --- .../Classes/Plugin/Playback/AVPlayerItemInfo.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/Clappr/Classes/Plugin/Playback/AVPlayerItemInfo.swift b/Sources/Clappr/Classes/Plugin/Playback/AVPlayerItemInfo.swift index f645ca5b7..1c94754b8 100644 --- a/Sources/Clappr/Classes/Plugin/Playback/AVPlayerItemInfo.swift +++ b/Sources/Clappr/Classes/Plugin/Playback/AVPlayerItemInfo.swift @@ -14,6 +14,7 @@ class AVPlayerItemInfo { } private unowned var delegate: AVPlayerItemInfoDelegate private var assetInfo: AVAssetInfo + private var becameReady = false private var durationLoaded: Bool { item.status == .readyToPlay @@ -64,9 +65,11 @@ class AVPlayerItemInfo { */ func waitForDuration() { observers = [ - item.observe(\.status) { [weak delegate] item, _ in - if item.status == .readyToPlay { - delegate?.didLoadDuration() + item.observe(\.status) { [weak self] item, _ in + guard let self = self else { return } + if item.status == .readyToPlay && !self.becameReady { + self.becameReady.toggle() + self.delegate.didLoadDuration() } } ] @@ -77,6 +80,7 @@ class AVPlayerItemInfo { } private func clearObservers() { + becameReady = false observers.forEach { $0.invalidate() } observers.removeAll() }