fix: Keep screen awake during playback

This commit is contained in:
PartyDonut 2025-10-15 18:51:13 +02:00
parent a8b2f9053e
commit 360b87dacb

View file

@ -3,7 +3,10 @@ package nl.jknaapen.fladder.player
import PlaybackState import PlaybackState
import android.app.ActivityManager import android.app.ActivityManager
import android.view.ViewGroup import android.view.ViewGroup
import android.view.WindowManager
import androidx.activity.compose.LocalActivity
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
@ -12,6 +15,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
@ -130,9 +134,19 @@ internal fun ExoPlayer(
} }
} }
val activity = LocalActivity.current
DisposableEffect(exoPlayer) { DisposableEffect(exoPlayer) {
val listener = object : Player.Listener { val listener = object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) { override fun onPlaybackStateChanged(playbackState: Int) {
activity?.window?.let {
if (exoPlayer.isPlaying) {
it.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
it.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
videoHost.setPlaybackState( videoHost.setPlaybackState(
PlaybackState( PlaybackState(
position = exoPlayer.currentPosition, position = exoPlayer.currentPosition,
@ -185,7 +199,8 @@ internal fun ExoPlayer(
) { showControls -> ) { showControls ->
AndroidView( AndroidView(
modifier = Modifier modifier = Modifier
.fillMaxSize(), .fillMaxSize()
.background(color = Color.Black),
factory = { factory = {
PlayerView(it).apply { PlayerView(it).apply {
player = exoPlayer player = exoPlayer
@ -194,6 +209,7 @@ internal fun ExoPlayer(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
) )
keepScreenOn = true
subtitleView?.apply { subtitleView?.apply {
setStyle( setStyle(
CaptionStyleCompat( CaptionStyleCompat(