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

Tin Tran

11/24/2021, 10:31 AM
Has anyone tried to animate the rotation of an AndroidView with exo player? I’m trying to animate the full screen action the video player similar to youtube.
Here’s what I’ve tried.
Copy code
@Preview
@Composable
fun Test() {

    Column(Modifier.fillMaxSize()) {
        var isLandScape by remember {
            mutableStateOf(true)
        }
        Button(onClick = { isLandScape = !isLandScape }) {
            Text(text = "test")
        }
        VideoPlayerTest(isLandScape)
    }

}


@Composable
fun VideoPlayerTest(isLandScape: Boolean) {
    // This is the official way to access current context from Composable functions
    val context = LocalContext.current

    // Do not recreate the player everytime this Composable commits
    val exoPlayer = remember(context) {
        SimpleExoPlayer.Builder(context).build().apply {
            val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(context,
                Util.getUserAgent(context, context.packageName))

            val source = ProgressiveMediaSource.Factory(dataSourceFactory)
                .createMediaSource(
                    Uri.parse(
                    // Big Buck Bunny from Blender Project
                    "<https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4>"
                ))

            this.prepare(source)
        }
    }

    BoxWithConstraints {
        AndroidView(
            factory = { context ->
                val view = LayoutInflater.from(context).inflate(R.layout.player, null) as StyledPlayerView
                view.apply {
                    player = exoPlayer
                }
            },
            Modifier
                .fillMaxSize()
                .rotate(if (isLandScape) 90f else 0f)
                .background(Color.Red)
        )
    }
}