@@ -38,6 +38,7 @@ import androidx.compose.material3.SliderDefaults
3838import androidx.compose.material3.Text
3939import androidx.compose.runtime.Composable
4040import androidx.compose.runtime.DisposableEffect
41+ import androidx.compose.runtime.LaunchedEffect
4142import androidx.compose.runtime.getValue
4243import androidx.compose.runtime.mutableFloatStateOf
4344import 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 }
0 commit comments