1
1
import android.annotation.SuppressLint
2
2
import android.content.pm.ActivityInfo
3
3
import android.os.Build
4
+ import android.os.Handler
5
+ import android.os.Looper
4
6
import android.view.View
5
7
import android.view.ViewGroup
6
8
import android.view.WindowInsets
@@ -202,7 +204,6 @@ fun YoutubeVideoPlayer(
202
204
val playerFragment = YouTubePlayerView (mContext)
203
205
var isFullScreen by remember { mutableStateOf(false ) }
204
206
205
-
206
207
val playerStateListener = object : AbstractYouTubePlayerListener () {
207
208
override fun onReady (youTubePlayer : com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer ) {
208
209
super .onReady(youTubePlayer)
@@ -214,35 +215,23 @@ fun YoutubeVideoPlayer(
214
215
youTubePlayer : com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer ,
215
216
state : PlayerConstants .PlayerState ,
216
217
) {
217
- super .onStateChange(youTubePlayer, state)
218
218
when (state) {
219
219
PlayerConstants .PlayerState .BUFFERING -> {
220
220
isLoading.invoke(true )
221
221
isPlaying.invoke(false )
222
222
}
223
-
224
223
PlayerConstants .PlayerState .PLAYING -> {
225
224
isLoading.invoke(false )
226
225
isPlaying.invoke(true )
227
226
}
228
-
229
227
PlayerConstants .PlayerState .ENDED -> {
230
228
isPlaying.invoke(false )
231
229
isLoading.invoke(false )
232
230
onVideoEnded.invoke()
233
231
}
234
-
235
232
else -> {}
236
233
}
237
234
}
238
-
239
- override fun onError (
240
- youTubePlayer : com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer ,
241
- error : PlayerConstants .PlayerError ,
242
- ) {
243
- super .onError(youTubePlayer, error)
244
- println (" iFramePlayer Error Reason = $error " )
245
- }
246
235
}
247
236
248
237
var fullscreenView: View ? by remember { mutableStateOf(null ) }
@@ -252,31 +241,34 @@ fun YoutubeVideoPlayer(
252
241
isFullScreen = true
253
242
fullscreenView = view
254
243
playerFragment.visibility = View .GONE
244
+
255
245
activity.requestedOrientation = ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE
256
246
257
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
258
- activity.window.setDecorFitsSystemWindows(false )
259
- activity.window.insetsController?.apply {
260
- hide(WindowInsets .Type .systemBars())
261
- systemBarsBehavior = WindowInsetsController .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
247
+ Handler (Looper .getMainLooper()).post {
248
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
249
+ activity.window.setDecorFitsSystemWindows(false )
250
+ activity.window.insetsController?.apply {
251
+ hide(WindowInsets .Type .systemBars())
252
+ systemBarsBehavior = WindowInsetsController .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
253
+ }
254
+ } else {
255
+ @Suppress(" DEPRECATION" )
256
+ activity.window.setFlags(
257
+ WindowManager .LayoutParams .FLAG_FULLSCREEN ,
258
+ WindowManager .LayoutParams .FLAG_FULLSCREEN
259
+ )
260
+ activity.window.decorView.systemUiVisibility =
261
+ View .SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
262
+ View .SYSTEM_UI_FLAG_HIDE_NAVIGATION or
263
+ View .SYSTEM_UI_FLAG_FULLSCREEN
262
264
}
263
- } else {
264
- @Suppress(" DEPRECATION" )
265
- activity.window.setFlags(
266
- WindowManager .LayoutParams .FLAG_FULLSCREEN ,
267
- WindowManager .LayoutParams .FLAG_FULLSCREEN
268
- )
269
- activity.window.decorView.systemUiVisibility =
270
- View .SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
271
- View .SYSTEM_UI_FLAG_HIDE_NAVIGATION or
272
- View .SYSTEM_UI_FLAG_FULLSCREEN
273
- }
274
265
275
- (activity.window.decorView as ViewGroup ).apply {
276
- addView(view)
266
+ (activity.window.decorView as ViewGroup ).apply {
267
+ addView(view)
268
+ }
277
269
}
278
270
279
- player?.play()
271
+ player?.play() // Start playing video after UI updates
280
272
}
281
273
282
274
override fun onExitFullscreen () {
@@ -285,24 +277,26 @@ fun YoutubeVideoPlayer(
285
277
286
278
playerFragment.visibility = View .VISIBLE
287
279
288
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
289
- activity.window.setDecorFitsSystemWindows(true )
290
- activity.window.insetsController?.apply {
291
- show(WindowInsets .Type .systemBars())
280
+ Handler (Looper .getMainLooper()).post {
281
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
282
+ activity.window.setDecorFitsSystemWindows(true )
283
+ activity.window.insetsController?.apply {
284
+ show(WindowInsets .Type .systemBars())
285
+ }
286
+ } else {
287
+ @Suppress(" DEPRECATION" )
288
+ activity.window.clearFlags(WindowManager .LayoutParams .FLAG_FULLSCREEN )
289
+ activity.window.decorView.systemUiVisibility = View .SYSTEM_UI_FLAG_VISIBLE
292
290
}
293
- } else {
294
- @Suppress(" DEPRECATION" )
295
- activity.window.clearFlags(WindowManager .LayoutParams .FLAG_FULLSCREEN )
296
- activity.window.decorView.systemUiVisibility = View .SYSTEM_UI_FLAG_VISIBLE
297
- }
298
291
299
- fullscreenView?.let { view ->
300
- (activity.window.decorView as ViewGroup ).apply {
301
- removeView(view)
292
+ fullscreenView?.let { view ->
293
+ (activity.window.decorView as ViewGroup ).apply {
294
+ removeView(view)
295
+ }
302
296
}
303
297
}
304
298
305
- player?.play()
299
+ player?.play() // Resume playing video after UI changes
306
300
}
307
301
}
308
302
@@ -345,17 +339,9 @@ fun YoutubeVideoPlayer(
345
339
val lifecycle = mLifeCycleOwner.lifecycle
346
340
val observer = LifecycleEventObserver { _, event ->
347
341
when (event) {
348
- Lifecycle .Event .ON_RESUME -> {
349
- player?.play()
350
- }
351
-
352
- Lifecycle .Event .ON_PAUSE -> {
353
- player?.pause()
354
- }
355
-
356
- else -> {
357
- //
358
- }
342
+ Lifecycle .Event .ON_RESUME -> player?.play()
343
+ Lifecycle .Event .ON_PAUSE -> player?.pause()
344
+ else -> {}
359
345
}
360
346
}
361
347
lifecycle.addObserver(observer)
0 commit comments