Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
fix: adjust observe AVItem status to call delegate only once
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorFerraz committed Mar 17, 2021
1 parent d009698 commit aef8617
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Sources/Clappr/Classes/Plugin/Playback/AVPlayerItemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
]
Expand All @@ -77,6 +80,7 @@ class AVPlayerItemInfo {
}

private func clearObservers() {
becameReady = false
observers.forEach { $0.invalidate() }
observers.removeAll()
}
Expand Down

0 comments on commit aef8617

Please sign in to comment.