Home > OS >  Composable function not being executed after mutable value change
Composable function not being executed after mutable value change

Time:11-03

So I got this line of code:



fun LiveTrainingScreen(viewModel: LiveTrainingViewModel = viewModel()) {


Column(modifier = Modifier.padding(PaddingStatic.Small).zIndex(2f)) {
    //Large Video Display
    //here
    var videoLink = remember { mutableStateOf(LiveTrainingViewModel.cockPitRight) }

    val exoPlayerCamera1 = viewModel.GetCameraPlayer(videoLink.value)


    DisposableEffect(
        AndroidView(
            modifier = Modifier
                .weight(1f)
                .fillMaxSize()
                .clip(RoundedCornerShape(RoundedSizeStatic.Medium))
                .clickable { videoLink = mutableStateOf(LiveTrainingViewModel.mapCamera) },
            factory = {
                PlayerView(viewModel.context).apply {
                    player = exoPlayerCamera1
                    useController = false
                    resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL

                    FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                }
            }
        )
    ) {
        onDispose {
            exoPlayerCamera1.release()
        }
    }
}
}

But when I click on the video element, the code is not being re-executed when I change the mediaItem Uri, because the video frame keeps displaying the same video.

And I don't understand what I am doing wrong.

Through mutablestate manual string change, re-execute code to change video display from the internet

CodePudding user response:

You are creating another mutableState (State<String>), instead of updating the current one the remember already calculated, and you also haven't specified any key to remember for it to re-calculate to tell your composable that something has changed for it to re-compose

...
.clickable { videoLink = mutableStateOf(LiveTrainingViewModel.mapCamera) }
...

Either you de-structure the value the remember returns like this, you can then have access to its value and a lambda that you can invoke to pass a new value for your composable to re-compose.

val (videoLinkValue, setValue) = remember { mutableStateOf(LiveTrainingViewModel.cockPitRight) }

updating it like this

...
.clickable { setValue (LiveTrainingViewModel.mapCamera) }
...

observing the value using the first de-structured component

val exoPlayerCamera1 = viewModel.GetCameraPlayer(videoLinkValue)

Or you can just simply do this

val videoLink by remember { mutableStateOf(LiveTrainingViewModel.cockPitRight) }

and just update it via .onClick like this

.clickable { videoLink = LiveTrainingViewModel.mapCamera }

CodePudding user response:

So for some reason exoplayer doesn't update when a State update is done.

So I tried to set the MediaItem for the second time which worked fine

.clickable {  exoPlayerCamera1.setMediaItem(MediaItem.fromUri(LiveTrainingViewModel.mapCamera))
},
  • Related