Skip to content

Commit ce5ae1b

Browse files
committed
Update LoopingPlayerProtocol.swift
1 parent f615c05 commit ce5ae1b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,28 @@ extension LoopingPlayerProtocol {
7272
/// - Parameters:
7373
/// - asset: The AVURLAsset to load into the player.
7474
func update(asset: AVURLAsset){
75-
// Optionally, check if the player is currently playing
76-
let wasPlaying = player?.rate != 0
7775

78-
// Pause the player if it was playing
76+
guard let player = player else { return }
77+
78+
let wasPlaying = player.rate != 0
79+
7980
if wasPlaying {
80-
player?.pause()
81+
player.pause()
8182
}
8283

8384
// Cleaning
8485
unloop()
85-
86+
while player.items().count > 0 {
87+
player.advanceToNextItem()
88+
}
8689
removeAllFilters()
8790

8891
// Replace the current item
8992
let newItem = AVPlayerItem(asset: asset)
90-
91-
player?.replaceCurrentItem(with: newItem)
92-
93+
player.replaceCurrentItem(with: newItem)
9394
loop()
9495

95-
player?.seek(to: .zero, completionHandler: { _ in
96+
player.seek(to: .zero, completionHandler: { _ in
9697
if wasPlaying {
9798
self.player?.play()
9899
}

0 commit comments

Comments
 (0)