androidx / androidx/media

Issue with Surface Handling when Switching Between Two ExoPlayers

Open
#1,433 3 comments 0 reactions 1 assignee View on GitHub

@microkatz is already working on this.

Since Jun 12, 2024.

needs triage question
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

I am implementing a scenario where I use two ExoPlayers to play different content on the same Surface, switching between them. The first player is used for the main content, and the second player provides explanations for certain terms. Here's the sequence of operations I follow:

Initialize and start the first player on a Surface.
Pause the first player and detach the Surface.
Initialize and start the second player on the same Surface.
When the second player finishes, detach the Surface and reattach it to the first player.
Resume the first player from where it left off.
Sometimes, after switching back to the first player, the video remains stuck on the initial frame, although the audio continues to play correctly.

Here's the code I am using:

// Initializing ExoPlayers
SimpleExoPlayer player1 = new SimpleExoPlayer.Builder(context).build();
SimpleExoPlayer player2 = new SimpleExoPlayer.Builder(context).build();

SurfaceView surfaceView = findViewById(R.id.surface_view);
Surface surface = surfaceView.getHolder().getSurface();

// Configuring the first player
player1.setVideoSurface(surface);
player1.setMediaItem(MediaItem.fromUri("first_url"));
player1.prepare();
player1.play();

// Pause the first player and switch to the second player
player1.pause();
player1.setVideoSurface(null); // Remove Surface

// Configuring the second player
player2.setVideoSurface(surface);
player2.setMediaItem(MediaItem.fromUri("second_url"));
player2.prepare();
player2.play();

// Handling end of playback for the second player
player2.addListener(new Player.Listener() {
    @Override
    public void onPlaybackStateChanged(int state) {
        if (state == Player.STATE_ENDED) {
            player2.setVideoSurface(null); // Remove Surface from the second player

            // Reassign the Surface to the first player and resume playback
            player1.setVideoSurface(surface);
            player1.play();
        }
    }
});

I am encountering an issue where, after the second player finishes and the Surface is reassigned to the first player, the video remains stuck on the initial frame while the audio plays correctly.

Additional Information:
If I call stop() and prepare() on the first ExoPlayer before resuming playback, it works correctly. However, I am looking for a more efficient way to handle this without stopping and preparing the player again.

Can you please help me identify what might be causing this issue and how to resolve it?

Steps to Reproduce:

Initialize and start the first player with video content.
Pause the first player and detach the Surface.
Initialize and start the second player on the same Surface.
Let the second player play to the end and then switch back to the first player.

Thank you for your assistance!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.