Search code examples
androidmedia-playerandroid-mediaplayeronresumeexoplayer

Android ExoPlayer becomes Green and Pixelated onResume()


In my Android app, I have a media player that implements the ExoPlayer interface.

When the home button is pressed, the player is supposed to pause. Then, when the app is relaunched from recent apps, the player should continue playing from where it left off.

Originally, I called player.release() in onPause() and got rid of the entire player instance. Then, when the app resumed, I created a new instance of the player and used seekTo() to jump to the correct position.

@Override
public void onPause() {
    if (player != null) {
        savePlayerPosition();
        player.release();
        player = null;
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (!player.isPlaying()) {
        player = ExoPlayerFactory.newSimpleInstance(context, trackSelector,
            new DefaultLoadControl(), drmSessionManager);

        setPlayerListeners();
        exoPlayerView.setPlayer(player);
        player.setPlayWhenReady(false);
        player.seekTo(playerWindow, playerPosition);
    }
}

The problem with the above code is that every time I loaded the app from recent apps, the player took a few moments to reconnect and load the video, instead of retaining the buffered data and right away resuming to play.

In order to solve this issue, I change my code to instead just toggle setPlayWhenReady().

@Override
public void onPause() {
    if (player != null) {
        player.setPlayWhenReady(false);
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (player != null && !player.isPlaying()) {
        player.setPlayWhenReady(true);
    }
}

This fixed the reloading issue and the video now resumes playing right away from where it left off. However, when the video resumes, for the first few seconds, the video is off color and pixelated, like shown in the picture below:

green screen

After the video plays for a few seconds, it resumes to the normal coloration and smooth appearance. I am seeing this issue when using an actual device as well as an emulator.

What is causing the player to get messed up? How can I fix or avoid this?


Solution

  • It seems like this issue was fixed with the release of ExoPlayer v2.3.0.

    It is still not perfect, though. Now, instead of turning green and pixelated, the audio begins playing a fraction before the video joins in. This jump is way less noticeable than a green screen, however, so I am considering this a fix.