I'm developing an android application which is collection of 100 sound effects. After I play for instance 25 of the sounds, I can't play anymore and I have to close the application and wait for some minutes then open the application and now I can play other sounds.
final MediaPlayer mp81 = MediaPlayer.create(MainActivity.this, R.raw.a81);
I play the sound using the below code:
mp81.start();
I stop the playing sound using the below code:
mp81.seekTo(0);
I also used stop()
method but the problem were still existing.
is there any other method i have to add?
Please note: consider using SoundPool for playing short sounds.
Regarding your use-case: you initialize your MediaPlayer
instance using the static create()
method which means you create a new MediaPlayer
object for each sound instead of reusing an existing instance and just changing the data source. This might negatively affect the performance of your app. I suggest that you create an array of paths to your sounds and then instantiate the MediaPlayer
like this:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(yourArray[x]);
mp.prepare();
mp.start();
} catch(Exception e){
e.printStackTrace();
}
Consult the MediaPlayer Document for more information.