Skip to content

Commit 01a3744

Browse files
committed
refactoring
1 parent 4e71fc2 commit 01a3744

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

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

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ public protocol AbstractPlayer: AnyObject {
9898
/// Sets the playback command for the video player.
9999
func setCommand(_ value: PlaybackCommand)
100100

101+
/// Applies the current set of filters to the video using an AVVideoComposition.
101102
func applyVideoComposition()
103+
104+
/// 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)?)
102106
}
103107

104108
extension AbstractPlayer{
@@ -138,66 +142,6 @@ extension AbstractPlayer{
138142
func pause() {
139143
player?.pause()
140144
}
141-
142-
/// Clears all items from the player's queue.
143-
func clearPlayerQueue() {
144-
guard let items = player?.items() else { return }
145-
for item in items {
146-
player?.remove(item)
147-
}
148-
}
149-
150-
/// Updates the current playback asset, settings, and initializes playback or a specific action when the asset is ready.
151-
///
152-
/// This method sets a new asset to be played, optionally loops it, and can automatically start playback.
153-
/// If provided, a callback is executed when the asset is ready to play.
154-
///
155-
/// - Parameters:
156-
/// - asset: The AVURLAsset to be loaded into the player.
157-
/// - loop: A Boolean value indicating whether the video should loop.
158-
/// - autoPlay: A Boolean value indicating whether playback should start automatically. Default is true.
159-
/// - callback: An optional closure to be called when the asset is ready to play.
160-
func update(asset: AVURLAsset, loop: Bool, autoPlay: Bool = true, callback: (() -> Void)? = nil) {
161-
162-
guard let player = player else { return }
163-
164-
let wasPlaying = player.rate != 0
165-
166-
if wasPlaying {
167-
pause()
168-
}
169-
170-
if !player.items().isEmpty {
171-
// Cleaning
172-
unloop()
173-
clearPlayerQueue()
174-
removeAllFilters()
175-
}
176-
177-
let newItem = AVPlayerItem(asset: asset)
178-
player.insert(newItem, after: nil)
179-
180-
if loop {
181-
self.loop()
182-
}
183-
184-
if let statusObserver{
185-
statusObserver.invalidate()
186-
}
187-
188-
if let callback{
189-
statusObserver = newItem.observe(\.status, options: [.new, .old]) { [weak self] item, change in
190-
guard item.status == .readyToPlay else { return }
191-
callback()
192-
self?.statusObserver?.invalidate()
193-
self?.statusObserver = nil
194-
}
195-
}
196-
197-
if autoPlay{
198-
player.play()
199-
}
200-
}
201145

202146
/// Seeks the video to a specific time.
203147
/// This method moves the playback position to the specified time with precise accuracy.

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,66 @@ internal extension LoopingPlayerProtocol {
122122
}
123123
}
124124

125+
/// Clears all items from the player's queue.
126+
func clearPlayerQueue() {
127+
guard let items = player?.items() else { return }
128+
for item in items {
129+
player?.remove(item)
130+
}
131+
}
132+
133+
/// Updates the current playback asset, settings, and initializes playback or a specific action when the asset is ready.
134+
///
135+
/// This method sets a new asset to be played, optionally loops it, and can automatically start playback.
136+
/// If provided, a callback is executed when the asset is ready to play.
137+
///
138+
/// - Parameters:
139+
/// - asset: The AVURLAsset to be loaded into the player.
140+
/// - loop: A Boolean value indicating whether the video should loop.
141+
/// - autoPlay: A Boolean value indicating whether playback should start automatically. Default is true.
142+
/// - 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) {
144+
145+
guard let player = player else { return }
146+
147+
let wasPlaying = player.rate != 0
148+
149+
if wasPlaying {
150+
pause()
151+
}
152+
153+
if !player.items().isEmpty {
154+
// Cleaning
155+
unloop()
156+
clearPlayerQueue()
157+
removeAllFilters()
158+
}
159+
160+
let newItem = AVPlayerItem(asset: asset)
161+
player.insert(newItem, after: nil)
162+
163+
if loop {
164+
self.loop()
165+
}
166+
167+
if let statusObserver{
168+
statusObserver.invalidate()
169+
}
170+
171+
if let callback{
172+
statusObserver = newItem.observe(\.status, options: [.new, .old]) { [weak self] item, change in
173+
guard item.status == .readyToPlay else { return }
174+
callback()
175+
self?.statusObserver?.invalidate()
176+
self?.statusObserver = nil
177+
}
178+
}
179+
180+
if autoPlay{
181+
player.play()
182+
}
183+
}
184+
125185
/// Sets up observers on the player item and the player to track their status and error states.
126186
///
127187
/// - Parameters:

0 commit comments

Comments
 (0)