Skip to content

Commit 3c762db

Browse files
authored
Merge pull request #104 from KhubaibKhan4/feature/yt-bug
-YouTube Player Bump Fixed (#93)
2 parents 4fe316c + cbcaa75 commit 3c762db

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

.kotlin/sessions/kotlin-compiler-12680122139031490636.salive

Whitespace-only changes.

mediaplayer-kmp/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ mavenPublishing {
142142
coordinates(
143143
groupId = "io.github.khubaibkhan4",
144144
artifactId = "mediaplayer-kmp",
145-
version = "2.0.8"
145+
version = "2.0.9"
146146
)
147147

148148
pom {

mediaplayer-kmp/src/androidMain/kotlin/YouTubePlayer.android.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import androidx.compose.material3.SliderDefaults
3838
import androidx.compose.material3.Text
3939
import androidx.compose.runtime.Composable
4040
import androidx.compose.runtime.DisposableEffect
41+
import androidx.compose.runtime.LaunchedEffect
4142
import androidx.compose.runtime.getValue
4243
import androidx.compose.runtime.mutableFloatStateOf
4344
import androidx.compose.runtime.mutableStateOf
@@ -221,8 +222,14 @@ fun YoutubeVideoPlayer(
221222
showControls: Boolean
222223
) {
223224
val context = LocalContext.current
225+
var activity by remember { mutableStateOf<Activity?>(null) }
226+
227+
LaunchedEffect(context) {
228+
activity = context as? Activity
229+
}
230+
231+
224232
val lifecycleOwner = androidx.lifecycle.compose.LocalLifecycleOwner.current
225-
val activity = context as Activity
226233
val videoId = extractVideoId(youtubeURL)
227234
val startTimeInSeconds = extractStartTime(youtubeURL)
228235

@@ -242,11 +249,12 @@ fun YoutubeVideoPlayer(
242249
fullscreenView = view
243250
playerView.visibility = View.GONE
244251

245-
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
252+
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
246253

247254
Handler(Looper.getMainLooper()).post {
248-
(activity.window.decorView as ViewGroup).addView(view)
249-
configureFullScreen(activity, true)
255+
(activity?.window?.decorView as ViewGroup).addView(view)
256+
activity?.let { configureFullScreen(it, true)}
257+
250258
}
251259
player?.play()
252260
}
@@ -255,13 +263,15 @@ fun YoutubeVideoPlayer(
255263
isFullScreen = false
256264
playerView.visibility = View.VISIBLE
257265
fullscreenView?.let { view ->
258-
(activity.window.decorView as ViewGroup).removeView(view)
266+
(activity?.window?.decorView as ViewGroup).removeView(view)
259267
fullscreenView = null
260268
}
261269

262270
Handler(Looper.getMainLooper()).post {
263-
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
264-
configureFullScreen(activity, false)
271+
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
272+
activity?.let {
273+
configureFullScreen(it, false)
274+
}
265275
}
266276
player?.play()
267277
}

sample/composeApp/src/commonMain/kotlin/sample/app/App.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fun MainScree(modifier: Modifier = Modifier) {
6464
.height(340.dp)
6565
.aspectRatio(16/9f)
6666
,
67-
url = "https://freetestdata.com/wp-content/uploads/2022/02/Free_Test_Data_1MB_MP4.mp4",
67+
url = "https://www.youtube.com/watch?v=AD2nEllUMJw",
6868
showControls = true,
6969
autoPlay = false
7070
)

0 commit comments

Comments
 (0)