Skip to content

Commit 712eb67

Browse files
committed
refactoring
1 parent 91cfa5e commit 712eb67

File tree

13 files changed

+60
-23
lines changed

13 files changed

+60
-23
lines changed

Sources/swiftui-loop-videoplayer/enum/Setting.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public enum Setting: Equatable, SettingsConvertible{
1919
[self]
2020
}
2121

22+
case loop
23+
2224
/// File name
2325
case name(String)
2426

@@ -53,6 +55,8 @@ public enum Setting: Equatable, SettingsConvertible{
5355
return nil
5456
}
5557

58+
print(firstChild)
59+
5660
return firstChild.value
5761
}
5862
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ public protocol LoopingPlayerProtocol: AbstractPlayer, LayerMakerProtocol{
3737
/// Declare a variable to hold the time observer token outside the if statement
3838
var timeObserverToken: Any? { get set }
3939

40-
/// Initializes a new instance of the view
40+
/// Initializes a new player view with specified video asset and configurations.
4141
///
4242
/// - Parameters:
43-
/// - asset: The AVURLAsset to be used in the player.
44-
/// - gravity: Specifies how the video content should be displayed within the layer bounds.
45-
/// - timePublishing: Optional CMTime that determines the interval at which the video current time should be published. Pass nil to disable time publishing.
46-
init(asset: AVURLAsset, gravity: AVLayerVideoGravity, timePublishing: CMTime?)
43+
/// - asset: The `AVURLAsset` used for video playback.
44+
/// - gravity: The `AVLayerVideoGravity` defining how the video content is displayed within the layer bounds.
45+
/// - timePublishing: Optional `CMTime` that specifies a particular time for publishing or triggering an event.
46+
/// - loop: A Boolean value that indicates whether the video should loop when playback reaches the end of the content.
47+
init(asset: AVURLAsset, gravity: AVLayerVideoGravity, timePublishing: CMTime?, loop : Bool)
4748

4849
/// Sets up the necessary observers on the AVPlayerItem and AVQueuePlayer to monitor changes and errors.
4950
///
@@ -82,7 +83,6 @@ internal extension LoopingPlayerProtocol {
8283
// Replace the current item
8384
let newItem = AVPlayerItem(asset: asset)
8485
player.insert(newItem, after: nil)
85-
loop()
8686
play()
8787
}
8888

@@ -95,14 +95,15 @@ internal extension LoopingPlayerProtocol {
9595
func setupPlayerComponents(
9696
asset: AVURLAsset,
9797
gravity: AVLayerVideoGravity,
98-
timePublishing: CMTime?
98+
timePublishing: CMTime?,
99+
loop: Bool
99100
) {
100101
let item = AVPlayerItem(asset: asset)
101102

102103
let player = AVQueuePlayer(items: [item])
103104
self.player = player
104105

105-
configurePlayer(player, gravity: gravity, timePublishing: timePublishing)
106+
configurePlayer(player, gravity: gravity, timePublishing: timePublishing, loop: loop)
106107

107108
setupObservers(for: item, player: player)
108109
}
@@ -116,7 +117,8 @@ internal extension LoopingPlayerProtocol {
116117
func configurePlayer(
117118
_ player: AVQueuePlayer,
118119
gravity: AVLayerVideoGravity,
119-
timePublishing: CMTime?
120+
timePublishing: CMTime?,
121+
loop : Bool
120122
) {
121123
player.isMuted = true
122124
playerLayer.player = player
@@ -134,7 +136,10 @@ internal extension LoopingPlayerProtocol {
134136
self.wantsLayer = true
135137
#endif
136138
compositeLayer.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height)
137-
loop()
139+
140+
if loop{
141+
self.loop()
142+
}
138143

139144
if !filters.isEmpty{ // have an idea for the feature
140145
applyVideoComposition()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public extension LoopPlayerViewProtocol{
8787
asset: AVURLAsset?) -> PlayerView? {
8888

8989
if let asset{
90-
let player = PlayerView(asset: asset, gravity: settings.gravity, timePublishing: settings.timePublishing)
90+
let player = PlayerView(asset: asset, gravity: settings.gravity, timePublishing: settings.timePublishing, loop: settings.loop)
9191
container.addSubview(player)
9292
activateFullScreenConstraints(for: player, in: container)
9393
return player
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Loop.swift
3+
//
4+
//
5+
// Created by Igor on 16.08.24.
6+
//
7+
8+
import Foundation
9+
10+
@available(iOS 14.0, macOS 11.0, tvOS 14.0, *)
11+
public struct Loop: SettingsConvertible{
12+
13+
// MARK: - Life circle
14+
15+
public init() {}
16+
17+
/// Fetch settings
18+
@_spi(Private)
19+
public func asSettings() -> [Setting] {
20+
[.loop]
21+
}
22+
}

Sources/swiftui-loop-videoplayer/utils/VideoSettings.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public struct VideoSettings: Equatable{
1919
/// Video extension
2020
public let ext: String
2121

22+
public let loop: Bool
23+
2224
/// A CMTime value representing the interval at which the player's current time should be published.
2325
/// If set, the player will publish periodic time updates based on this interval.
2426
public let timePublishing: CMTime?
@@ -61,6 +63,8 @@ public struct VideoSettings: Equatable{
6163
errorFontSize = settings.fetch(by : "errorFontSize", defaulted: 17)
6264

6365
timePublishing = settings.fetch(by : "timePublishing", defaulted: nil)
66+
67+
loop = settings.contains(.loop)
6468
}
6569
}
6670

Sources/swiftui-loop-videoplayer/view/loop/player/ios/LoopingPlayerUIView.swift renamed to Sources/swiftui-loop-videoplayer/view/player/ios/LoopingPlayerUIView.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
4848
/// The delegate to be notified about errors encountered by the player.
4949
weak var delegate: PlayerDelegateProtocol?
5050

51-
/// Initializes a new instance of the view
51+
/// Initializes a new player view with specified video asset and configurations.
5252
///
5353
/// - Parameters:
54-
/// - asset: The AVURLAsset to be used in the player.
55-
/// - gravity: Specifies how the video content should be displayed within the layer bounds.
56-
/// - timePublishing: Optional CMTime that determines the interval at which the video current time should be published. Pass nil to disable time publishing.
57-
required init(asset: AVURLAsset, gravity: AVLayerVideoGravity, timePublishing: CMTime?){
54+
/// - asset: The `AVURLAsset` used for video playback.
55+
/// - gravity: The `AVLayerVideoGravity` defining how the video content is displayed within the layer bounds.
56+
/// - timePublishing: Optional `CMTime` that specifies a particular time for publishing or triggering an event.
57+
/// - loop: A Boolean value that indicates whether the video should loop when playback reaches the end of the content.
58+
required init(asset: AVURLAsset, gravity: AVLayerVideoGravity, timePublishing: CMTime?, loop: Bool){
5859
super.init(frame: .zero)
5960
setupPlayerComponents(
60-
asset: asset, gravity: gravity, timePublishing : timePublishing
61+
asset: asset, gravity: gravity, timePublishing : timePublishing, loop: loop
6162
)
6263
}
6364

Sources/swiftui-loop-videoplayer/view/loop/player/mac/LoopingPlayerNSView.swift renamed to Sources/swiftui-loop-videoplayer/view/player/mac/LoopingPlayerNSView.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ class LoopingPlayerNSView: NSView, LoopingPlayerProtocol {
5050
/// The delegate to be notified about errors encountered by the player.
5151
weak var delegate: PlayerDelegateProtocol?
5252

53-
/// Initializes a new instance of the view
53+
/// Initializes a new player view with specified video asset and configurations.
5454
///
5555
/// - Parameters:
56-
/// - asset: The AVURLAsset to be used in the player.
57-
/// - gravity: Specifies how the video content should be displayed within the layer bounds.
58-
/// - timePublishing: Optional CMTime that determines the interval at which the video current time should be published. Pass nil to disable time publishing.
59-
required init(asset: AVURLAsset, gravity: AVLayerVideoGravity, timePublishing: CMTime?) {
56+
/// - asset: The `AVURLAsset` used for video playback.
57+
/// - gravity: The `AVLayerVideoGravity` defining how the video content is displayed within the layer bounds.
58+
/// - timePublishing: Optional `CMTime` that specifies a particular time for publishing or triggering an event.
59+
/// - loop: A Boolean value that indicates whether the video should loop when playback reaches the end of the content.
60+
required init(asset: AVURLAsset, gravity: AVLayerVideoGravity, timePublishing: CMTime?, loop : Bool) {
6061
super.init(frame: .zero)
6162
setupPlayerComponents(
62-
asset: asset, gravity: gravity, timePublishing: timePublishing
63+
asset: asset, gravity: gravity, timePublishing: timePublishing, loop: loop
6364
)
6465
}
6566

0 commit comments

Comments
 (0)