@@ -18,7 +18,7 @@ import AppKit
1818/// handle errors, and notify a delegate of important events.
1919@available ( iOS 14 , macOS 11 , tvOS 14 , * )
2020@MainActor
21- public protocol LoopingPlayerProtocol : AnyObject {
21+ public protocol LoopingPlayerProtocol : AbstractPlayer {
2222
2323 var playerLayer : AVPlayerLayer { get }
2424
@@ -32,9 +32,6 @@ public protocol LoopingPlayerProtocol: AnyObject {
3232 /// The looper responsible for continuous video playback.
3333 var playerLooper : AVPlayerLooper ? { get set }
3434
35- /// The queue player that plays the video items.
36- var player : AVQueuePlayer ? { get set }
37-
3835 /// The delegate to be notified about errors encountered by the player.
3936 var delegate : PlayerErrorDelegate ? { get set }
4037
@@ -48,6 +45,10 @@ public protocol LoopingPlayerProtocol: AnyObject {
4845 /// ensuring that all playback errors are managed and reported appropriately.
4946 var errorObserver : NSKeyValueObservation ? { get set }
5047
48+ /// Initializes a video player with a specified media asset and layer gravity.
49+ /// - Parameters:
50+ /// - asset: The `AVURLAsset` representing the media content to be played. This asset encapsulates the properties of the media file.
51+ /// - gravity: The `AVLayerVideoGravity` that determines how the video content is displayed within the bounds of the player layer. Common values are `.resizeAspect`, `.resizeAspectFill`, and `.resize` to control the scaling and filling behavior of the video content.
5152 init ( asset: AVURLAsset , gravity: AVLayerVideoGravity )
5253
5354 /// Sets up the necessary observers on the AVPlayerItem and AVQueuePlayer to monitor changes and errors.
@@ -66,123 +67,10 @@ public protocol LoopingPlayerProtocol: AnyObject {
6667 ///
6768 /// - Parameter player: The AVQueuePlayer that encountered an error.
6869 func handlePlayerError( _ player: AVPlayer )
69-
70- /// Configures the provided AVQueuePlayer with specific properties for video playback.
71- /// - Parameters:
72- /// - player: The AVQueuePlayer to be configured.
73- /// - gravity: The AVLayerVideoGravity determining how the video content should be scaled or fit within the player layer.
74- func configurePlayer( _ player: AVQueuePlayer , gravity: AVLayerVideoGravity )
75-
76- // Playback control methods
77-
78- /// Initiates or resumes playback of the video.
79- /// This method should be implemented to start playing the video from its current position.
80- func play( )
81-
82- /// Pauses the current video playback.
83- /// This method should be implemented to pause the video, allowing it to be resumed later from the same position.
84- func pause( )
85-
86- /// Seeks the video to a specific time.
87- /// This method moves the playback position to the specified time with precise accuracy.
88- /// - Parameter time: The target time to seek to in the video timeline.
89- func seek( to time: Double )
90-
91- /// Seeks to the start of the video.
92- /// This method positions the playback at the beginning of the video.
93- func seekToStart( )
94-
95- /// Seeks to the end of the video.
96- /// This method positions the playback at the end of the video.
97- func seekToEnd( )
98-
99- /// Mutes the video playback.
100- /// This method silences the audio of the video.
101- func mute( )
102-
103- /// Unmutes the video playback.
104- /// This method restores the audio of the video.
105- func unmute( )
106-
10770}
10871
10972extension LoopingPlayerProtocol {
110-
111- // Implementations of playback control methods
112-
113- /// Initiates playback of the video.
114- /// This method starts or resumes playing the video from the current position.
115- func play( ) {
116- player? . play ( )
117- }
118-
119- /// Pauses the video playback.
120- /// This method pauses the video if it is currently playing, allowing it to be resumed later from the same position.
121- func pause( ) {
122- player? . pause ( )
123- }
12473
125- /// Seeks the video to a specific time.
126- /// This method moves the playback position to the specified time with precise accuracy.
127- /// - Parameter time: The target time to seek to in the video timeline.
128- func seek( to time: Double ) {
129- seekToTime ( player: player, seekTimeInSeconds: time)
130- }
131-
132- /// Seeks to the start of the video.
133- /// This method positions the playback at the beginning of the video.
134- func seekToStart( ) {
135- seekToTime ( player: player, seekTimeInSeconds: 0 )
136- }
137-
138- /// Seeks to the end of the video.
139- /// This method positions the playback at the end of the video.
140- func seekToEnd( ) {
141- if let duration = player? . currentItem? . duration {
142- let endTime = CMTimeGetSeconds ( duration)
143- seekToTime ( player: player, seekTimeInSeconds: endTime)
144- }
145- }
146-
147- /// Mutes the video playback.
148- /// This method silences the audio of the video.
149- func mute( ) {
150- player? . isMuted = true
151- }
152-
153- /// Unmutes the video playback.
154- /// This method restores the audio of the video.
155- func unmute( ) {
156- player? . isMuted = false
157- }
158-
159- /// Sets the playback command for the video player.
160- /// - Parameter value: The `PlaybackCommand` to set. This can be one of the following:
161- /// - `play`: Command to play the video.
162- /// - `pause`: Command to pause the video.
163- /// - `seek(to:)`: Command to seek to a specific time in the video.
164- /// - `begin`: Command to position the video at the beginning.
165- /// - `end`: Command to position the video at the end.
166- /// - `mute`: Command to mute the video.
167- /// - `unmute`: Command to unmute the video.
168- func setCommand( _ value: PlaybackCommand ) {
169- switch value {
170- case . play:
171- play ( )
172- case . pause:
173- pause ( )
174- case . seek( to: let time) :
175- seek ( to: time)
176- case . begin:
177- seekToStart ( )
178- case . end:
179- seekToEnd ( )
180- case . mute:
181- mute ( )
182- case . unmute:
183- unmute ( )
184- }
185- }
18674
18775 /// Sets up the player components using the provided asset and video gravity.
18876 ///
0 commit comments