Skip to content

Commit 03cac2a

Browse files
committed
refactoring
1 parent 2849f9d commit 03cac2a

File tree

2 files changed

+130
-117
lines changed

2 files changed

+130
-117
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// AbstractPlayer.swift
3+
//
4+
//
5+
// Created by Igor on 07.08.24.
6+
//
7+
8+
import AVFoundation
9+
import Foundation
10+
11+
@available(iOS 14, macOS 11, tvOS 14, *)
12+
public protocol AbstractPlayer: AnyObject{
13+
/// The queue player that plays the video items.
14+
var player: AVQueuePlayer? { get set }
15+
16+
// Playback control methods
17+
18+
/// Initiates or resumes playback of the video.
19+
/// This method should be implemented to start playing the video from its current position.
20+
func play()
21+
22+
/// Pauses the current video playback.
23+
/// This method should be implemented to pause the video, allowing it to be resumed later from the same position.
24+
func pause()
25+
26+
/// Seeks the video to a specific time.
27+
/// This method moves the playback position to the specified time with precise accuracy.
28+
/// - Parameter time: The target time to seek to in the video timeline.
29+
func seek(to time: Double)
30+
31+
/// Seeks to the start of the video.
32+
/// This method positions the playback at the beginning of the video.
33+
func seekToStart()
34+
35+
/// Seeks to the end of the video.
36+
/// This method positions the playback at the end of the video.
37+
func seekToEnd()
38+
39+
/// Mutes the video playback.
40+
/// This method silences the audio of the video.
41+
func mute()
42+
43+
/// Unmutes the video playback.
44+
/// This method restores the audio of the video.
45+
func unmute()
46+
}
47+
48+
extension AbstractPlayer{
49+
50+
// Implementations of playback control methods
51+
52+
/// Initiates playback of the video.
53+
/// This method starts or resumes playing the video from the current position.
54+
func play() {
55+
player?.play()
56+
}
57+
58+
/// Pauses the video playback.
59+
/// This method pauses the video if it is currently playing, allowing it to be resumed later from the same position.
60+
func pause() {
61+
player?.pause()
62+
}
63+
64+
/// Seeks the video to a specific time.
65+
/// This method moves the playback position to the specified time with precise accuracy.
66+
/// - Parameter time: The target time to seek to in the video timeline.
67+
func seek(to time: Double) {
68+
seekToTime(player: player, seekTimeInSeconds: time)
69+
}
70+
71+
/// Seeks to the start of the video.
72+
/// This method positions the playback at the beginning of the video.
73+
func seekToStart() {
74+
seekToTime(player: player, seekTimeInSeconds: 0)
75+
}
76+
77+
/// Seeks to the end of the video.
78+
/// This method positions the playback at the end of the video.
79+
func seekToEnd() {
80+
if let duration = player?.currentItem?.duration {
81+
let endTime = CMTimeGetSeconds(duration)
82+
seekToTime(player: player, seekTimeInSeconds: endTime)
83+
}
84+
}
85+
86+
/// Mutes the video playback.
87+
/// This method silences the audio of the video.
88+
func mute() {
89+
player?.isMuted = true
90+
}
91+
92+
/// Unmutes the video playback.
93+
/// This method restores the audio of the video.
94+
func unmute() {
95+
player?.isMuted = false
96+
}
97+
98+
/// Sets the playback command for the video player.
99+
/// - Parameter value: The `PlaybackCommand` to set. This can be one of the following:
100+
/// - `play`: Command to play the video.
101+
/// - `pause`: Command to pause the video.
102+
/// - `seek(to:)`: Command to seek to a specific time in the video.
103+
/// - `begin`: Command to position the video at the beginning.
104+
/// - `end`: Command to position the video at the end.
105+
/// - `mute`: Command to mute the video.
106+
/// - `unmute`: Command to unmute the video.
107+
func setCommand(_ value: PlaybackCommand) {
108+
switch value {
109+
case .play:
110+
play()
111+
case .pause:
112+
pause()
113+
case .seek(to: let time):
114+
seek(to: time)
115+
case .begin:
116+
seekToStart()
117+
case .end:
118+
seekToEnd()
119+
case .mute:
120+
mute()
121+
case .unmute:
122+
unmute()
123+
}
124+
}
125+
}

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

Lines changed: 5 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -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

10972
extension 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

Comments
 (0)