Best way to release and reinitialize exoplayer in RecyclerView

Viewed 2056

I have two instances of Exoplayer with different mediasources.

Currently

Before initializing either of player. I am checking if any player attached to the surface if yes then releasing the player and initializing again with new operator

if(videoPlayerView.getPlayer() != null) {
             videoPlayerView.getPlayer().release();
             videoPlayerView.setPlayer(null);
 }

videoPlayerView is my PlayerView in SimpleExoPlayer

But after View detached list item becomes black instead of playing again.

2 Answers

I used to do similar things with you.You need a global players pool to manager players.You needn't to release it everytime.Just call stop method.And when switch video,just reset source.To make it play instantly witout black, you need to prepare it.When activity is finishing, release all players.

I strongly suggest you have a single instance of Exoplayer, init in first use and release when activity or fragment (based on your usecase) destroyed

Related