ExoPlayer in Jetpack Compose shows black screen when app is resumed after video is done playing
See original GitHub issueExoPlayer 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
}
}
)
}
- Play any video to the end (after which the last frame is kept on screen)
- Turn off the screen of the device
- 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:
- Created a year ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We can’t really advise you on Compose details, but the important things that your code should ensure are:
onStop
on API >= 24 andonPause
on API <= 23.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 aLifecycleEventObserver
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.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.