https://kotlinlang.org logo
#compose
Title
# compose
r

reactormonk

09/19/2023, 5:52 PM
Does this code look correct? I'm trying to debug on why on recompose the exoplayer doesn't display anymore.
Copy code
val context = LocalContext.current
    val playerView = remember {
        val layout = LayoutInflater.from(context).inflate(R.layout.video_player_auto, null, false)
        val playerView = layout.findViewById(R.id.playerView) as PlayerView
        playerView.apply {
            player = exoPlayer
        }
    }

    Log.d("VideoPlayer", "View: $playerView")

    AndroidView(
        { playerView },
        Modifier
            .fillMaxHeight()
            .background(Color.Black)
    )
Do I have to release the inflated view somehow?
j

jw

09/19/2023, 5:54 PM
Is there a reason you are explicitly remembering the view rather than inflating it within the factory lambda of
AndroidView
? Are you trying to retain it across changes to the
Context
?
r

reactormonk

09/19/2023, 5:55 PM
No, no specific reason. Copypasta from somewhere I suspect.
... fixed.
This seems to work better:
Copy code
@Composable
fun VideoPlayer(
    exoPlayer: ExoPlayer,
) {
    KeepScreenOn()
    AndroidView(
        {
            val layout = LayoutInflater.from(it).inflate(R.layout.video_player_auto, null, false)
            val playerView = layout.findViewById(R.id.playerView) as PlayerView
            playerView.apply {
                player = exoPlayer
                Log.d("VideoPlayer", "view: $this")
            }
        },
        Modifier
            .fillMaxHeight()
            .background(Color.Black),
        {
            it.player = exoPlayer
        }
    )
}
Although I think I should be reusing the
exoPlayer
instance 🤔
v

Vlad

09/20/2023, 8:11 AM
Is there something wrong remembering a View out of the factory? Sometimes we want to call methods of that View. For example I might have a WebView running and at some point I will want it to evaluate JS
j

jw

09/20/2023, 3:36 PM
That is what the update callback is for. State reads will cause recomposition and you can do writes to the view state imperatively.