Skip to content

Commit 901ccbc

Browse files
committed
update
1 parent 01a3744 commit 901ccbc

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public protocol AbstractPlayer: AnyObject {
102102
func applyVideoComposition()
103103

104104
/// Updates the current playback asset, settings, and initializes playback or a specific action when the asset is ready.
105-
func update(asset: AVURLAsset, loop: Bool, autoPlay: Bool, callback: (() -> Void)?)
105+
func update(asset: AVURLAsset, loop: Bool, autoPlay: Bool, callback: ((AVPlayerItem.Status) -> Void)?)
106106
}
107107

108108
extension AbstractPlayer{
@@ -155,8 +155,12 @@ extension AbstractPlayer{
155155

156156
guard player.currentItem?.status == .readyToPlay else{
157157
if let asset = currentAsset{
158-
update(asset: asset , loop: false, autoPlay: false){[weak self] in
159-
self?.seek(to: time)
158+
update(asset: asset , loop: false, autoPlay: false){ [weak self] status in
159+
if status == .readyToPlay{
160+
self?.seek(to: time)
161+
}else {
162+
self?.delegate?.didSeek(value: false, currentTime: time)
163+
}
160164
}
161165
return
162166
}

Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ internal extension LoopingPlayerProtocol {
140140
/// - loop: A Boolean value indicating whether the video should loop.
141141
/// - autoPlay: A Boolean value indicating whether playback should start automatically. Default is true.
142142
/// - callback: An optional closure to be called when the asset is ready to play.
143-
func update(asset: AVURLAsset, loop: Bool, autoPlay: Bool = true, callback: (() -> Void)? = nil) {
143+
func update(asset: AVURLAsset, loop: Bool, autoPlay: Bool = true, callback: ((AVPlayerItem.Status) -> Void)? = nil) {
144144

145145
guard let player = player else { return }
146146

@@ -164,14 +164,17 @@ internal extension LoopingPlayerProtocol {
164164
self.loop()
165165
}
166166

167-
if let statusObserver{
168-
statusObserver.invalidate()
169-
}
167+
statusObserver?.invalidate()
168+
170169

171170
if let callback{
172171
statusObserver = newItem.observe(\.status, options: [.new, .old]) { [weak self] item, change in
173-
guard item.status == .readyToPlay else { return }
174-
callback()
172+
//.unknown: This state is essentially the default, indicating that the player item is new or has not yet attempted to load its assets.
173+
guard item.status == .readyToPlay || item.status == .failed else {
174+
return
175+
}
176+
177+
callback(item.status)
175178
self?.statusObserver?.invalidate()
176179
self?.statusObserver = nil
177180
}

0 commit comments

Comments
 (0)