@@ -12,6 +12,12 @@ import CoreImage
12
12
@MainActor
13
13
public protocol AbstractPlayer : AnyObject {
14
14
15
+ /// Retrieves the current item being played.
16
+ var currentItem : AVPlayerItem ? { get }
17
+
18
+ /// The current asset being played, if available.
19
+ var currentAsset : AVURLAsset ? { get }
20
+
15
21
/// Adjusts the brightness of the video. Default is 0 (no change), with positive values increasing and negative values decreasing brightness.
16
22
var brightness : Float { get set }
17
23
@@ -86,6 +92,28 @@ public protocol AbstractPlayer: AnyObject {
86
92
}
87
93
88
94
extension AbstractPlayer {
95
+
96
+ /// Retrieves the current item being played.
97
+ ///
98
+ /// This computed property checks if there is a current item available in the player.
99
+ /// If available, it returns the `currentItem`; otherwise, it returns `nil`.
100
+ var currentItem : AVPlayerItem ? {
101
+ if let currentItem = player? . currentItem {
102
+ return currentItem
103
+ }
104
+ return nil
105
+ }
106
+
107
+ /// The current asset being played, if available.
108
+ ///
109
+ /// This computed property checks the current item of the player.
110
+ /// If the current item exists and its asset can be cast to AVURLAsset,
111
+ var currentAsset : AVURLAsset ? {
112
+ if let currentItem = currentItem {
113
+ return currentItem. asset as? AVURLAsset
114
+ }
115
+ return nil
116
+ }
89
117
90
118
// Implementations of playback control methods
91
119
@@ -135,7 +163,7 @@ extension AbstractPlayer{
135
163
/// Seeks to the end of the video.
136
164
/// This method positions the playback at the end of the video.
137
165
func seekToEnd( ) {
138
- if let duration = player ? . currentItem? . duration {
166
+ if let duration = currentItem? . duration {
139
167
let endTime = CMTimeGetSeconds ( duration)
140
168
seek ( to: endTime)
141
169
}
@@ -175,8 +203,8 @@ extension AbstractPlayer{
175
203
/// Pass `nil` to turn off subtitles.
176
204
func setSubtitles( to language: String ? ) {
177
205
#if !os(visionOS)
178
- guard let currentItem = player ? . currentItem,
179
- let group = currentItem . asset . mediaSelectionGroup ( forMediaCharacteristic: . legible) else {
206
+ guard let currentItem = currentItem,
207
+ let group = currentAsset ? . mediaSelectionGroup ( forMediaCharacteristic: . legible) else {
180
208
return
181
209
}
182
210
@@ -274,7 +302,7 @@ extension AbstractPlayer{
274
302
if wasPlaying {
275
303
player. pause ( )
276
304
}
277
-
305
+
278
306
player. items ( ) . forEach { item in
279
307
280
308
let videoComposition = AVVideoComposition ( asset: item. asset, applyingCIFiltersWithHandler: { request in
@@ -312,10 +340,10 @@ extension AbstractPlayer{
312
340
/// Selects an audio track for the video playback.
313
341
/// - Parameter languageCode: The language code (e.g., "en" for English) of the desired audio track.
314
342
func selectAudioTrack( languageCode: String ) {
315
- guard let currentItem = player ? . currentItem else { return }
343
+ guard let currentItem = currentItem else { return }
316
344
#if !os(visionOS)
317
345
// Retrieve the media selection group for audible tracks
318
- if let group = currentItem . asset . mediaSelectionGroup ( forMediaCharacteristic: . audible) {
346
+ if let group = currentAsset ? . mediaSelectionGroup ( forMediaCharacteristic: . audible) {
319
347
320
348
// Filter options by language code using Locale
321
349
let options = group. options. filter { option in
0 commit comments