Skip to content

Commit 5df34a0

Browse files
committed
refactoring
1 parent b7e48f4 commit 5df34a0

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import CoreImage
1212
@MainActor
1313
public protocol AbstractPlayer: AnyObject {
1414

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+
1521
/// Adjusts the brightness of the video. Default is 0 (no change), with positive values increasing and negative values decreasing brightness.
1622
var brightness: Float { get set }
1723

@@ -86,6 +92,28 @@ public protocol AbstractPlayer: AnyObject {
8692
}
8793

8894
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+
}
89117

90118
// Implementations of playback control methods
91119

@@ -135,7 +163,7 @@ extension AbstractPlayer{
135163
/// Seeks to the end of the video.
136164
/// This method positions the playback at the end of the video.
137165
func seekToEnd() {
138-
if let duration = player?.currentItem?.duration {
166+
if let duration = currentItem?.duration {
139167
let endTime = CMTimeGetSeconds(duration)
140168
seek(to: endTime)
141169
}
@@ -175,8 +203,8 @@ extension AbstractPlayer{
175203
/// Pass `nil` to turn off subtitles.
176204
func setSubtitles(to language: String?) {
177205
#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 {
180208
return
181209
}
182210

@@ -274,7 +302,7 @@ extension AbstractPlayer{
274302
if wasPlaying {
275303
player.pause()
276304
}
277-
305+
278306
player.items().forEach{ item in
279307

280308
let videoComposition = AVVideoComposition(asset: item.asset, applyingCIFiltersWithHandler: { request in
@@ -312,10 +340,10 @@ extension AbstractPlayer{
312340
/// Selects an audio track for the video playback.
313341
/// - Parameter languageCode: The language code (e.g., "en" for English) of the desired audio track.
314342
func selectAudioTrack(languageCode: String) {
315-
guard let currentItem = player?.currentItem else { return }
343+
guard let currentItem = currentItem else { return }
316344
#if !os(visionOS)
317345
// 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) {
319347

320348
// Filter options by language code using Locale
321349
let options = group.options.filter { option in

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,6 @@ public protocol LoopingPlayerProtocol: AbstractPlayer{
6868

6969
extension LoopingPlayerProtocol {
7070

71-
/// The current asset being played, if available.
72-
///
73-
/// This computed property checks the current item of the player.
74-
/// If the current item exists and its asset can be cast to AVURLAsset,
75-
var currentAsset : AVURLAsset?{
76-
if let currentItem = player?.currentItem {
77-
return currentItem.asset as? AVURLAsset
78-
}
79-
return nil
80-
}
81-
8271
/// Updates the player to play a new asset and handles the playback state.
8372
///
8473
/// This method pauses the player if it was previously playing,

0 commit comments

Comments
 (0)