Skip to content

Commit f3cbfa2

Browse files
committed
update
1 parent 3ddad6e commit f3cbfa2

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public protocol AbstractPlayer: AnyObject {
110110
func applyVideoComposition()
111111

112112
/// Updates the current playback asset, settings, and initializes playback or a specific action when the asset is ready.
113-
func update(settings: VideoSettings, doUpdate : Bool)
113+
func update(settings: VideoSettings, doUpdate : Bool, callback : ((AVPlayerItem) -> Void)?)
114114
}
115115

116116
extension AbstractPlayer{
@@ -215,10 +215,9 @@ extension AbstractPlayer{
215215

216216
/// Clear status observer
217217
func clearStatusObserver(){
218-
if statusObserver != nil{
219-
statusObserver?.invalidate()
220-
statusObserver = nil
221-
}
218+
guard statusObserver != nil else { return }
219+
statusObserver?.invalidate()
220+
statusObserver = nil
222221
}
223222

224223
/// Creates an `AVPlayerItem` with optional subtitle merging.
@@ -259,19 +258,21 @@ extension AbstractPlayer{
259258
delegate?.didSeek(value: false, currentTime: time)
260259
return
261260
}
262-
update(settings: settings, doUpdate: true)
263261
let callback : (AVPlayerItem.Status) -> Void = { [weak self] status in
264262
guard status == .readyToPlay else {
265263
self?.delegate?.didSeek(value: false, currentTime: time)
266264
return
267265
}
268266
self?.seek(to: time, play: play)
269267
}
270-
/// Need to refactor
271-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){ [weak self] in
272-
guard let item = self?.currentItem else { return }
273-
self?.setupStateStatusObserver(for: item, callback: callback)
268+
269+
update(settings: settings, doUpdate: true){ [weak self] item in
270+
/// Need to refactor
271+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
272+
self?.setupStateStatusObserver(for: item, callback: callback)
273+
}
274274
}
275+
275276
return
276277
}
277278

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,27 @@ internal extension ExtPlayerProtocol {
156156
#endif
157157
}
158158

159-
/// Updates the player with a new asset and applies specified video settings.
160-
/// Initializes playback or performs a specified action once the asset is ready.
159+
/// Updates the player with a new asset and applies the specified video settings.
161160
///
162-
/// This method sets a new `AVURLAsset` to be played based on the provided settings.
163-
/// It can configure looping and muting options, and automatically starts playback if specified.
164-
/// A callback is executed when the asset transitions to the `.readyToPlay` status, allowing for
165-
/// further actions dependent on the readiness of the asset.
161+
/// This method sets a new `AVURLAsset` for playback and configures it according to the provided settings.
162+
/// It can adjust options such as playback gravity, looping, and muting. If `doUpdate` is `true`, the player is
163+
/// updated immediately with the new asset. The method also provides an optional callback that is executed when
164+
/// the asset transitions to the `.readyToPlay` status, enabling additional actions to be performed once the
165+
/// player item is ready for playback.
166166
///
167167
/// - Parameters:
168-
/// - settings: A `VideoSettings` struct containing configurations such as playback gravity,
169-
/// whether to loop the content, and whether to mute the audio.
170-
func update(settings: VideoSettings, doUpdate : Bool = false) {
168+
/// - settings: A `VideoSettings` struct containing configurations such as playback gravity, looping behavior,
169+
/// and whether the audio should be muted.
170+
/// - doUpdate: A `Bool` value indicating whether the player should update immediately with the new asset.
171+
/// Defaults to `false`, meaning the player will not change unless explicitly triggered.
172+
/// - callback: An optional closure that takes an `AVPlayerItem` as its parameter. This is called when the
173+
/// player item transitions to the `.readyToPlay` status, allowing for additional customization
174+
/// or actions once the asset is prepared.
175+
func update(
176+
settings: VideoSettings,
177+
doUpdate : Bool = false,
178+
callback : ((AVPlayerItem) -> Void)? = nil
179+
) {
171180

172181
if doUpdate == false && settings.isEqual(currentSettings){
173182
return
@@ -181,6 +190,8 @@ internal extension ExtPlayerProtocol {
181190
return
182191
}
183192

193+
callback?(newItem)
194+
184195
insert(newItem)
185196

186197
if settings.loop {

0 commit comments

Comments
 (0)