Search code examples
androidmedia-player

Android - Too many instances of MediaPlayer


I have an activity that has 12 buttons, and each one plays a separate file. I have created 12 different MediaPlayers and set them with the correct file like this:

music = MediaPlayer.create(this, R.raw.music);

This worked fine for just a few instances, but now it says I have too many instances and the program will just crash before anything happens. I have tried just creating one MediaPlayer instance and just try to change the file the it plays when the button is pressed by using the create method like usual, but this doesn't work.

Is there a way to do this or have any ideas?


Solution

  • Make music as Singleton.. Reuse the same object created. this may sort out your problem.