media: Can't interact with ExoPlayer using Jetpack Compose on Android TV

Media3 Version

ExoPlayer 2.18.7

Devices that reproduce the issue

Emulator running Android TV API 31

Devices that do not reproduce the issue

None

Reproducible in the demo app?

Yes

Reproduction steps

Using androidx.tv version 1.0.0-alpha06 with the following composable (adding focusable and focusRequester were my attempts to fix it):

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun VideoPlayer(mediaItem: VideoApi.MediaItem) {
    val (playerFocus) = remember { FocusRequester.createRefs() }
    val context = LocalContext.current

    val exoPlayer = remember {
        ExoPlayer.Builder(context)
            .build()
            .apply {
                addMediaItem(
                    MediaItem.Builder()
                        .setUri(mediaItem.hlsCaptions)
                        .setMimeType(MimeTypes.APPLICATION_M3U8)
                        .build()
                )
                prepare()
                playWhenReady = true
            }
    }

    LaunchedEffect(Unit) {
        playerFocus.requestFocus()
    }

    DisposableEffect(
        AndroidView(
            modifier = Modifier
                .focusable(true)
                .focusRequester(playerFocus),
            factory = {
                StyledPlayerView(context).apply {
                    player = exoPlayer
                    useController = true
                    controllerAutoShow = true
                    setShowBuffering(StyledPlayerView.SHOW_BUFFERING_ALWAYS)
                }
            }
        )
    ) {
        onDispose {
            exoPlayer.release()
        }
    }
}

Expected result

Should be able to interact, i.e. D-pad should bring up video HUD controls.

Actual result

Any key presses (besides back) do nothing.

Media

Any media should show it. I’m using an HLS stream. For example: https://m.wsj.net/video/20230531/eaa54145-d07a-4100-8153-2eaf8d671921/1/hls/manifest-hd-wifi.m3u8

Bug Report

  • You will email the zip file produced by adb bugreport to dev.exoplayer@gmail.com after filing this issue.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

@taschmidt Stream them in a Flow. Inject the flow into the Activity and also in the ViewModel/whatever constructor.

DPAD means you only have a limited set of inputs “events”, which makes it hacky, but “reasonable” (I claim) to pass them down from the Activity if Compose cannot receive them (eg: system keys that come into the app via an Intent instead of a KeyEvent).