@@ -15,14 +15,22 @@ import SwiftUI
15
15
public protocol LoopPlayerViewProtocol {
16
16
17
17
#if os(iOS) || os(tvOS)
18
- associatedtype View : UIView
18
+ associatedtype View : UIView
19
19
#elseif os(macOS)
20
20
associatedtype View : NSView
21
21
#else
22
22
associatedtype View : CustomView
23
23
#endif
24
24
25
25
associatedtype ErrorView
26
+
27
+ #if os(iOS) || os(tvOS)
28
+ associatedtype PlayerView : LoopingPlayerProtocol , UIView
29
+ #elseif os(macOS)
30
+ associatedtype PlayerView : LoopingPlayerProtocol , NSView
31
+ #else
32
+ associatedtype PlayerView : LoopingPlayerProtocol , CustomView
33
+ #endif
26
34
27
35
/// Settings for configuring the video player.
28
36
var settings : Settings { get }
@@ -58,62 +66,24 @@ public extension LoopPlayerViewProtocol{
58
66
activateFullScreenConstraints ( for: errorView, in: view)
59
67
}
60
68
}
61
-
62
- /// Adds a subview to a given view and activates full-screen constraints on the subview within the parent view.
63
- /// - Parameters:
64
- /// - view: The parent view to which the subview will be added.
65
- /// - subView: The subview that will be added to the parent view.
66
- @MainActor
67
- func compose( _ view: View , _ subView: View ) {
68
- view. addSubview ( subView)
69
- activateFullScreenConstraints ( for: subView, in: view)
70
- }
71
- }
72
-
73
- #if os(iOS) || os(tvOS)
74
- @available ( iOS 14 , tvOS 14 , * )
75
- public extension LoopPlayerViewProtocol where Self: UIViewRepresentable , Context == UIViewRepresentableContext < Self > {
76
69
77
70
/// Creates a player view for looping video content.
78
71
/// - Parameters:
79
72
/// - context: The UIViewRepresentable context providing environment data and coordinator.
80
73
/// - asset: The AVURLAsset to be used for video playback.
81
74
/// - Returns: A PlayerView instance conforming to LoopingPlayerProtocol.
82
75
@MainActor
83
- func createPlayerView < PlayerView : LoopingPlayerProtocol > (
84
- context : Context ,
85
- asset: AVURLAsset ) -> PlayerView {
76
+ func makePlayerView (
77
+ _ container : View ,
78
+ asset: AVURLAsset ? ) -> PlayerView ? {
86
79
80
+ if let asset{
81
+ let player = PlayerView ( asset: asset, gravity: settings. gravity)
82
+ container. addSubview ( player)
83
+ activateFullScreenConstraints ( for: player, in: container)
84
+ return player
85
+ }
87
86
88
- let player = PlayerView ( asset: asset, gravity: settings. gravity)
89
-
90
- player. delegate = context. coordinator as? PlayerErrorDelegate
91
- return player
92
- }
93
- }
94
-
95
- #endif
96
-
97
- #if os(macOS)
98
- @available ( macOS 11 , * )
99
- public extension LoopPlayerViewProtocol where Self: NSViewRepresentable , Context == NSViewRepresentableContext < Self > {
100
-
101
- /// Creates a player view for looping video content.
102
- /// - Parameters:
103
- /// - context: The NSViewRepresentable context providing environment data and coordinator.
104
- /// - asset: The AVURLAsset to be used for video playback.
105
- /// - Returns: A PlayerView instance conforming to LoopingPlayerProtocol.
106
- @MainActor
107
- func createPlayerView< PlayerView: LoopingPlayerProtocol > (
108
- context: Context ,
109
- asset: AVURLAsset ) -> PlayerView {
110
-
111
- let player = PlayerView ( asset: asset, gravity: settings. gravity)
112
-
113
- player. delegate = context. coordinator as? PlayerErrorDelegate
114
- return player
87
+ return nil
115
88
}
116
89
}
117
- #endif
118
-
119
-
0 commit comments