Search code examples
javaandroidandroid-recyclerviewandroid-mediaplayer

How to manage multiple instances of mediaplayer?


I have one MediaPlayer for each item in the RecyclerView, each item has is own play/pause button. what I'm trying to do is:

  1. When one of the media players is playing and its button is pressed, pause the player. when its button is pressed, start the player.

  2. If the user tries to start another player, stop the player which is playing and start the new one.


EDIT

Solved by using ExoPlayer (will add code later)


Solution

  • As you are trying to play inside a list, I would suggest you to create the mediaplayer instance only when user press the play button. and when user pause or stop the playing destroy the mediaplayer instance. In this way your list scrolling would also be smooth. If you try to instanciate multiple instance of the mediaplayer I am afraid you won't get a smooth performance.

    So what you would do, you would create a wrapper class of the android mediaplayer that would control the playing, pausing, volume etc functionalities. When user ask for any action to happen like press the play button, you would tell this class to handle the responsibilities.

    One more thing try using ExoPlayer instead of mediaplayer.