androidandroid-jetpack-composeexoplayerandroid-video-player

ExoPlayer shaking badly during recompositions on Android API 30+


I have tested on different API levels. It happens on API 30+ but on lower API it works fine. The problem exists on emulators and physical devices.

Here is brief code of how I declare Player and work with it:

[versions]
exoplayer = "1.3.1"

[libraries]
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "exoplayer" }
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "exoplayer" }

ViewModel

@HiltViewModel
class NotesViewModel @Inject constructor(
    @ApplicationContext val context: Context,
) : ViewModel() {
    val player: Player = buildPlayer(context).apply {
        repeatMode = Player.REPEAT_MODE_ONE
        prepare()
    }

}

UI

 val viewModel: NotesViewModel = hiltViewModel()
 val player = remember { viewModel.player }
 VideoPlayer(
    player = player,
    isPlaying = uiState.isVideoPlaying,
    onPlayClicked = viewModel::playVideo,
 )

@OptIn(UnstableApi::class)
@Composable
fun VideoPlayer(
    player: Player,
    isPlaying: Boolean,
    onPlayClicked: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val videoHeight = screenHeight() * VIDEO_HEIGHT_RATIO

    Box(modifier) {
        AndroidView(
            factory = { ctx ->
                PlayerView(ctx).apply {
                    this.player = player
                    useController = false
                    resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIXED_HEIGHT
                }
            },
            modifier = Modifier
                .fillMaxWidth()
                .height(videoHeight.dp)
        )
        PlayButton(
            visible = !isPlaying,
            onClick = onPlayClicked,
            modifier = Modifier.align(Alignment.Center)
        )
    }
}

[Fine behavior with API lower 30][Shaking with API 30+]


Solution

  • Self-answer

    Setting TextureView fixes the issue