@@ -40,6 +40,7 @@ import platform.CoreGraphics.CGPoint
4040import platform.CoreGraphics.CGRectGetMidX
4141import platform.CoreGraphics.CGRectGetMidY
4242import platform.Foundation.NSCoder
43+ import platform.UIKit.NSLayoutConstraint
4344import platform.UIKit.UICollectionViewDelegateProtocol
4445import platform.UIKit.UIColor.Companion.blackColor
4546import platform.UIKit.UINib
@@ -53,46 +54,64 @@ import platform.darwin.NSObject
5354
5455@Composable
5556actual fun VideoPlayer (
56- modifier : Modifier ,
57- url : String ,
58- autoPlay : Boolean
57+ modifier : Modifier , url : String , autoPlay : Boolean , showControls : Boolean
5958) {
6059 if (url.contains(" youtube.com" ) || url.contains(" youtu.be" )) {
61- YouTubeIFramePlayer (url = url, modifier = modifier,autoPlay = autoPlay)
60+ YouTubeIFramePlayer (url = url, modifier = modifier,autoPlay = autoPlay, showControls = showControls )
6261 } else if (isVideoFile(url) || isAudioFile(url)) {
63- AvPlayerView (modifier,url,autoPlay)
62+ AvPlayerView (modifier,url,autoPlay, showControls = showControls )
6463 } else {
6564 Text (" Unsupported media format" , modifier = modifier)
6665 }
6766}
6867
6968@Composable
70- fun AvPlayerView (modifier : Modifier = Modifier , url : String , autoPlay : Boolean ) {
69+ fun AvPlayerView (
70+ modifier : Modifier = Modifier ,
71+ url : String ,
72+ autoPlay : Boolean ,
73+ showControls : Boolean
74+ ) {
75+ val validUrl = remember(url) { NSURL .URLWithString (url) }
76+
7177 val player = remember {
72- NSURL . URLWithString (url.toString()) ?.let { AVPlayer (uRL = it) }
78+ validUrl ?.let { AVPlayer (uRL = it) }
7379 }
74- val playerLayer = remember { AVPlayerLayer () }
80+
7581 val avPlayerViewController = remember { AVPlayerViewController () }
82+
7683 avPlayerViewController.player = player
77- avPlayerViewController.showsPlaybackControls = true
78- avPlayerViewController.allowsPictureInPicturePlayback = true
84+ avPlayerViewController.showsPlaybackControls = showControls
85+ avPlayerViewController.allowsPictureInPicturePlayback = showControls
7986
80- playerLayer.player = player
8187 UIKitView (
8288 factory = {
8389 val playerContainer = UIView ()
90+
91+ avPlayerViewController.view.translatesAutoresizingMaskIntoConstraints = false
8492 playerContainer.addSubview(avPlayerViewController.view)
93+
94+ NSLayoutConstraint .activateConstraints(
95+ listOf (
96+ avPlayerViewController.view.leadingAnchor.constraintEqualToAnchor(playerContainer.leadingAnchor),
97+ avPlayerViewController.view.trailingAnchor.constraintEqualToAnchor(playerContainer.trailingAnchor),
98+ avPlayerViewController.view.topAnchor.constraintEqualToAnchor(playerContainer.topAnchor),
99+ avPlayerViewController.view.bottomAnchor.constraintEqualToAnchor(playerContainer.bottomAnchor)
100+ )
101+ )
102+
85103 playerContainer
86104 },
87105 modifier = modifier,
88- update = { view ->
89- when (autoPlay){
90- true -> player?.play()
91- false -> {
92-
93- }
106+ update = { _ ->
107+ if (autoPlay) {
108+ player?.play()
109+ } else {
110+ player?.pause()
94111 }
95- avPlayerViewController.player?.play()
112+ },
113+ onRelease = {
114+ player?.pause()
96115 },
97116 properties = UIKitInteropProperties (
98117 isInteractive = true ,
@@ -130,19 +149,18 @@ actual fun MediaPlayer(
130149 volumeIconColor : Color ,
131150 playIconColor : Color ,
132151 sliderTrackColor : Color ,
133- sliderIndicatorColor : Color
152+ sliderIndicatorColor : Color ,
153+ showControls : Boolean ,
134154) {
135155 val player = remember {
136156 createAVPlayerWithHeaders(url, headers)
137157 }
138- val playerLayer = remember { AVPlayerLayer () }
139158 val avPlayerViewController = remember { AVPlayerViewController () }
140159
160+ // Configure the player and controls based on `showControls`
141161 avPlayerViewController.player = player
142- avPlayerViewController.showsPlaybackControls = true
143- avPlayerViewController.allowsPictureInPicturePlayback = true
144-
145- playerLayer.player = player
162+ avPlayerViewController.showsPlaybackControls = showControls
163+ avPlayerViewController.allowsPictureInPicturePlayback = showControls
146164
147165 UIKitView (
148166 factory = {
@@ -154,6 +172,8 @@ actual fun MediaPlayer(
154172 update = { _ ->
155173 if (autoPlay) {
156174 player?.play()
175+ } else {
176+ player?.pause()
157177 }
158178 },
159179 onRelease = {
@@ -178,14 +198,13 @@ private fun createAVPlayerWithHeaders(url: String, headers: Map<String, String>)
178198}
179199
180200@Composable
181- fun YouTubeIFramePlayer (url : String , modifier : Modifier ,autoPlay : Boolean ) {
201+ fun YouTubeIFramePlayer (url : String , modifier : Modifier , autoPlay : Boolean , showControls : Boolean ) {
182202 val videoId = remember(url) {
183- url?.substringAfter(" v=" )?.substringBefore(" &" ) ? : url?.substringAfterLast(" /" )
184- }
185- val isAutoPlay = when (autoPlay){
186- true -> 1
187- false -> 0
203+ url.substringAfter(" v=" ).substringBefore(" &" ).ifEmpty { url.substringAfterLast(" /" ) }
188204 }
205+ val isAutoPlay = if (autoPlay) 1 else 0
206+ val controls = if (showControls) 1 else 0
207+
189208 val htmlContent = """
190209 <html>
191210 <head>
@@ -214,8 +233,9 @@ fun YouTubeIFramePlayer(url: String, modifier: Modifier,autoPlay: Boolean) {
214233 <body>
215234 <div class="video-container">
216235 <iframe
217- src="https://www.youtube.com/embed/$videoId ?autoplay=$isAutoPlay "
218- allow="autoplay;">
236+ src="https://www.youtube.com/embed/$videoId ?autoplay=$isAutoPlay &controls=$controls "
237+ allow="autoplay;"
238+ frameborder="0">
219239 </iframe>
220240 </div>
221241 </body>
@@ -231,9 +251,9 @@ fun YouTubeIFramePlayer(url: String, modifier: Modifier,autoPlay: Boolean) {
231251 },
232252 modifier = modifier
233253 .fillMaxWidth()
234- .aspectRatio(16f / 13f ),
254+ .aspectRatio(16f / 9f ),
235255 update = { view ->
236- // Update logic if needed
256+
237257 },
238258 properties = UIKitInteropProperties (
239259 isInteractive = true ,
0 commit comments