Skip to content

Commit af17c71

Browse files
committed
clean resources on update
1 parent 327cf41 commit af17c71

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ extension AbstractPlayer{
307307

308308
/// Removes all filters from the video playback.
309309
func removeAllFilters(apply : Bool = true) {
310+
311+
guard !filters.isEmpty else { return }
312+
310313
filters = []
311314
if apply{
312315
applyVideoComposition()

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ extension LoopingPlayerProtocol {
9696
player?.pause()
9797
}
9898

99+
removeStatusObserver()
100+
unloop()
101+
removeAllFilters()
102+
99103
// Replace the current item with a new item created from the asset
100104
let newItem = AVPlayerItem(asset: asset)
101-
unloop()
102-
if !filters.isEmpty{
103-
removeAllFilters()
104-
}
105+
106+
addStatusObserver(for: newItem)
107+
105108
player?.replaceCurrentItem(with: newItem)
106109

107110
// Seek to the beginning of the item if you want to start from the start
@@ -168,14 +171,22 @@ extension LoopingPlayerProtocol {
168171
/// - item: The player item to observe.
169172
/// - player: The player to observe.
170173
func setupObservers(for item: AVPlayerItem, player: AVQueuePlayer) {
171-
statusObserver = item.observe(\.status, options: [.new]) { [weak self] item, _ in
172-
self?.handlePlayerItemStatusChange(item)
173-
}
174-
174+
addStatusObserver(for: item)
175175
errorObserver = player.observe(\.error, options: [.new]) { [weak self] player, _ in
176176
self?.handlePlayerError(player)
177177
}
178178
}
179+
180+
func removeStatusObserver(){
181+
statusObserver?.invalidate()
182+
statusObserver = nil
183+
}
184+
185+
func addStatusObserver(for item: AVPlayerItem){
186+
statusObserver = item.observe(\.status, options: [.new]) { [weak self] item, _ in
187+
self?.handlePlayerItemStatusChange(item)
188+
}
189+
}
179190

180191
/// Responds to changes in the status of an AVPlayerItem.
181192
///

0 commit comments

Comments
 (0)