@@ -164,25 +164,38 @@ internal extension LoopingPlayerProtocol {
164
164
self . loop ( )
165
165
}
166
166
167
+ // Set up state item status observer
168
+ setupStateItemStatusObserver ( newItem: newItem, callback: callback)
169
+
170
+ if autoPlay{
171
+ player. play ( )
172
+ }
173
+ }
174
+
175
+ /// Sets up an observer for the status of the provided `AVPlayerItem`.
176
+ ///
177
+ /// This method observes changes in the status of `newItem` and triggers the provided callback
178
+ /// whenever the status changes to `.readyToPlay` or `.failed`. Once the callback is invoked,
179
+ /// the observer is invalidated, ensuring that the callback is called only once.
180
+ ///
181
+ /// - Parameters:
182
+ /// - newItem: The `AVPlayerItem` whose status is to be observed.
183
+ /// - callback: A closure that is called when the item's status changes to `.readyToPlay` or `.failed`.
184
+ func setupStateItemStatusObserver( newItem: AVPlayerItem , callback: ( ( AVPlayerItem . Status ) -> Void ) ? ) {
167
185
statusObserver? . invalidate ( )
168
186
169
-
170
- if let callback{
171
- statusObserver = newItem. observe ( \. status, options: [ . new, . old] ) { [ weak self] item, change in
172
- //.unknown: This state is essentially the default, indicating that the player item is new or has not yet attempted to load its assets.
187
+ if let callback = callback {
188
+ //.unknown: This state is essentially the default, indicating that the player item is new or has not yet attempted to load its assets.
189
+ statusObserver = newItem. observe ( \. status, options: [ . new, . old] ) { [ weak self] item, _ in
173
190
guard item. status == . readyToPlay || item. status == . failed else {
174
191
return
175
192
}
176
193
177
- callback ( item. status)
178
- self ? . statusObserver? . invalidate ( )
179
- self ? . statusObserver = nil
194
+ callback ( item. status)
195
+ self ? . statusObserver? . invalidate ( )
196
+ self ? . statusObserver = nil
180
197
}
181
198
}
182
-
183
- if autoPlay{
184
- player. play ( )
185
- }
186
199
}
187
200
188
201
/// Sets up observers on the player item and the player to track their status and error states.
0 commit comments