'Change video based on state - ExoPLayer - Jetpack Compose

I have an application that display a certain background video based on a state collected from DataBase.

The problem is that when i'm doing this, the videos don't want to be played, and i don't know how to achieve this.

How can achieve play certain video based on state with smooth transition between theme

This is my actual code ->

BackgroundPLayer

val context = LocalContext.current
val userAgent = Util.getUserAgent(context, "AppName")
val defaultDataSourceFactory = DefaultDataSourceFactory(context, userAgent)

val eclipse = RawResourceDataSource.buildRawResourceUri(R.raw.background_eclipse)
val mediaSourceEclipse = ProgressiveMediaSource.Factory(defaultDataSourceFactory)
    .createMediaSource(MediaItem.fromUri(eclipse))

val hyperloop = RawResourceDataSource.buildRawResourceUri(R.raw.hyperloop)
val mediaSourceHyperloop = ProgressiveMediaSource.Factory(defaultDataSourceFactory)
    .createMediaSource(MediaItem.fromUri(hyperloop))

PlayerView(
    modifier = modifier,
    context = context,
    dataConsumptionState = dataConsumptionState,
    mediaSourceEclipse = mediaSourceEclipse,
    mediaSourceHyperloop = mediaSourceHyperloop
)

PlayerView

val exoPlayer = remember(context) {
    ExoPlayer.Builder(context).build()
        .apply {
            setMediaSource(
                mediaSourceBasedOnState(
                dataConsumptionState = dataConsumptionState,
                mediaSourceEclipse = mediaSourceEclipse,
                mediaSourceHyperloop = mediaSourceHyperloop
            ), 0)
            prepare()
            repeatMode = Player.REPEAT_MODE_ONE
            playWhenReady = true
        }
}

val playerView = StyledPlayerView(context).apply {
    player = exoPlayer
}

AndroidView(
    modifier = modifier
        .fillMaxHeight(),
    factory = {
        playerView
    }
)

mediaSourceBasedOnState

return when (dataConsumptionState) {
    DataConsumptionState.INUSE -> {
        mediaSourceHyperloop
    }
    DataConsumptionState.ENDED -> {
        mediaSourceEclipse
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source