question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExoPlayer in Jetpack Compose shows black screen when app is resumed after video is done playing

See original GitHub issue

ExoPlayer Version

2.18.0

Devices that reproduce the issue

Samsung Galaxy S21 Ultra

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

Not tested

Reproduction steps

Jetpack Compose implementation:

@Composable
fun VideoLayout(@RawRes rawResource: Int) {
    val context = LocalContext.current
    val uri = RawResourceDataSource.buildRawResourceUri(rawResource)
    val mediaItem = MediaItem.fromUri(uri)
    val exoPlayer = remember(context, mediaItem) {
        ExoPlayer.Builder(context).build().apply {
            addMediaItem(mediaItem)
            prepare()
            play()
            repeatMode = REPEAT_MODE_OFF
        }
    }

    DisposableEffect(exoPlayer) { onDispose { exoPlayer.release() } }

    AndroidView(
        modifier = Modifier.fillMaxSize(),
        factory = {
            StyledPlayerView(it).apply {
                player = exoPlayer
                resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
                useController = false
            }
        }
    )
}
  1. Play any video to the end (after which the last frame is kept on screen)
  2. Turn off the screen of the device
  3. Turn on the screen of the device

Expected result

The player keeps showing the last frame

Actual result

The player is showing a black screen

Media

Any video will do. Short videos are probably preferred since you need to play them to the end to reproduce the bug.

Bug Report

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
toniheicommented, Jul 28, 2022

We can’t really advise you on Compose details, but the important things that your code should ensure are:

  • The player needs to be released when the Activity goes into background (=when the screen is turned off). In a normal activity this should happen in onStop on API >= 24 and onPause on API <= 23.
  • When the activity comes back into foreground (onStart on API 24+, onResume on API <=23) the player needs to be re-created from scratch and you can optionally seek to the previous playback position to resume from where you left off.

You can see this happening in the demo app (e.g. here, here and here), but I’m not sure how this can be translated to Compose. You may want to manage the lifecycle of the player outside of the composable function and only assign it to the created StyledPlayerView when needed.

Based on your description it sounds as if the player might not be correctly released when going into background or is not rebuilt when coming back into foreground. For example, based on the official documentation, it sounds like onDispose is not the right method to clean-up and it should rather happen inside a LifecycleEventObserver to match the requirements I listed above. At this point it becomes a generic question not really related to ExoPlayer, and you may get better answers by asking on sites like StackOverflow.

0reactions
toniheicommented, Oct 7, 2022

I think I close this issue as it seems more related to general Android developement. Built-in support for Compose is tracked by https://github.com/google/ExoPlayer/issues/9654.

Read more comments on GitHub >

github_iconTop Results From Across the Web

For ExoPlayer in Jetpack Compose, the player shows a black ...
For ExoPlayer in Jetpack Compose, the player shows a black screen after pausing and resuming the app for non-looping videos.
Read more >
Learn with code: Jetpack Compose — Playing Media (Part 3)
I have given the background colours for top and bottom half as black and grey to illustrate the screen segregation. Previewing the composable ......
Read more >
Playing video by ExoPlayer - Medium
Exoplayer features are play video and audio, shuffle, repeat, subtitle, playlist, caching/downloading, playing ads, live streaming, album art, offline, ...
Read more >
Activity state changes - Android Developers
When an app enters multi-window mode, available in Android 7.0 (API level 24)and higher, the system notifies the currently running activity of a ......
Read more >
Exoplayer in Jetpack Compose ( Play video from URLs) | Kotlin
Hey Guys, In this video we are going to learn to play video from URL in Jetpack Compose App.Github: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found