Skip to content

Commit 50cca32

Browse files
committed
refactoring
1 parent 44731e4 commit 50cca32

File tree

3 files changed

+44
-48
lines changed

3 files changed

+44
-48
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@
77

88
import AVFoundation
99
import Foundation
10-
10+
#if canImport(UIKit)
11+
import UIKit
12+
#elseif canImport(AppKit)
13+
import AppKit
14+
#endif
1115
/// A protocol defining the requirements for a looping video player.
1216
///
1317
/// Conforming types are expected to manage a video player that can loop content continuously,
1418
/// handle errors, and notify a delegate of important events.
1519
@available(iOS 14, macOS 11, tvOS 14, *)
1620
@MainActor
1721
public protocol LoopingPlayerProtocol: AnyObject {
22+
23+
var playerLayer : AVPlayerLayer { get }
24+
25+
#if canImport(UIKit)
26+
var layer : CALayer { get }
27+
#elseif canImport(AppKit)
28+
var layer : CALayer? { get set }
29+
var wantsLayer : Bool { get set }
30+
#endif
31+
1832
/// The looper responsible for continuous video playback.
1933
var playerLooper: AVPlayerLooper? { get set }
2034

@@ -123,6 +137,33 @@ extension LoopingPlayerProtocol {
123137
setupObservers(for: item, player: player)
124138
}
125139

140+
/// Configures the provided AVQueuePlayer with specific properties for video playback.
141+
///
142+
/// This method sets the video gravity and muted state of the player, and assigns it to a player layer.
143+
/// It is intended to set up the player with the necessary configuration for video presentation based on the given gravity.
144+
/// - Parameters:
145+
/// - player: The AVQueuePlayer to be configured.
146+
/// - gravity: The AVLayerVideoGravity determining how the video content should be scaled or fit within the player layer.
147+
internal func configurePlayer(_ player: AVQueuePlayer, gravity: AVLayerVideoGravity) {
148+
player.isMuted = true
149+
playerLayer.player = player
150+
playerLayer.videoGravity = gravity
151+
#if canImport(UIKit)
152+
playerLayer.backgroundColor = UIColor.clear.cgColor
153+
layer.addSublayer(playerLayer)
154+
#elseif canImport(AppKit)
155+
playerLayer.backgroundColor = NSColor.clear.cgColor
156+
layer = CALayer()
157+
layer?.addSublayer(playerLayer)
158+
wantsLayer = true
159+
#endif
160+
161+
if let firstItem = player.items().first {
162+
playerLooper = AVPlayerLooper(player: player, templateItem: firstItem)
163+
}
164+
player.play()
165+
}
166+
126167
/// Sets up observers on the player item and the player to track their status and error states.
127168
///
128169
/// - Parameters:

Sources/swiftui-loop-videoplayer/view/loop/ios/LoopingPlayerUIView.swift

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import UIKit
1919
class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
2020

2121
/// The AVPlayerLayer that displays the video content.
22-
private let playerLayer = AVPlayerLayer()
22+
internal let playerLayer = AVPlayerLayer()
2323

2424
/// The looper responsible for continuous video playback.
2525
internal var playerLooper: AVPlayerLooper?
@@ -50,26 +50,6 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
5050
fatalError("init(coder:) has not been implemented")
5151
}
5252

53-
/// Configures the provided AVQueuePlayer with specific properties for video playback.
54-
///
55-
/// This method sets the video gravity and muted state of the player, and assigns it to a player layer.
56-
/// It is intended to set up the player with the necessary configuration for video presentation based on the given gravity.
57-
/// - Parameters:
58-
/// - player: The AVQueuePlayer to be configured.
59-
/// - gravity: The AVLayerVideoGravity determining how the video content should be scaled or fit within the player layer.
60-
internal func configurePlayer(_ player: AVQueuePlayer, gravity: AVLayerVideoGravity) {
61-
player.isMuted = true
62-
playerLayer.player = player
63-
playerLayer.videoGravity = gravity
64-
playerLayer.backgroundColor = UIColor.clear.cgColor
65-
layer.addSublayer(playerLayer)
66-
67-
if let firstItem = player.items().first {
68-
playerLooper = AVPlayerLooper(player: player, templateItem: firstItem)
69-
}
70-
player.play()
71-
}
72-
7353
/// Lays out subviews and adjusts the frame of the player layer to match the view's bounds.
7454
override func layoutSubviews() {
7555
super.layoutSubviews()
@@ -86,6 +66,3 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
8666
}
8767
}
8868
#endif
89-
90-
91-

Sources/swiftui-loop-videoplayer/view/loop/mac/LoopingPlayerNSView.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import AppKit
2121
class LoopingPlayerNSView: NSView, LoopingPlayerProtocol {
2222

2323
/// The AVPlayerLayer that displays the video content.
24-
private let playerLayer = AVPlayerLayer()
24+
internal let playerLayer = AVPlayerLayer()
2525

2626
/// The looper responsible for continuous video playback.
2727
internal var playerLooper: AVPlayerLooper?
@@ -51,28 +51,6 @@ class LoopingPlayerNSView: NSView, LoopingPlayerProtocol {
5151
required init?(coder: NSCoder) {
5252
fatalError("init(coder:) has not been implemented")
5353
}
54-
55-
/// Configures the player with settings for looping, muted playback, and visual layout.
56-
///
57-
/// This method sets the video gravity and muted state of the player, and assigns it to a player layer.
58-
/// It is intended to set up the player with the necessary configuration for video presentation based on the given gravity.
59-
/// - Parameters:
60-
/// - player: The AVQueuePlayer to be configured.
61-
/// - gravity: The AVLayerVideoGravity determining how the video content should be scaled or fit within the player layer.
62-
internal func configurePlayer(_ player: AVQueuePlayer, gravity: AVLayerVideoGravity) {
63-
player.isMuted = true
64-
playerLayer.player = player
65-
playerLayer.videoGravity = gravity
66-
playerLayer.backgroundColor = NSColor.clear.cgColor
67-
layer = CALayer()
68-
layer?.addSublayer(playerLayer)
69-
wantsLayer = true
70-
71-
if let firstItem = player.items().first {
72-
playerLooper = AVPlayerLooper(player: player, templateItem: firstItem)
73-
}
74-
player.play()
75-
}
7654

7755
/// Lays out subviews and adjusts the frame of the player layer to match the view's bounds.
7856
override func layout() {

0 commit comments

Comments
 (0)