Reputation: 4267
In my app i added a sound when a button is clicked
This is the class that manages the sound
public class GestoreSuoni{
MediaPlayer mp;
public void playSound(Context context,int sound){
mp = MediaPlayer.create(context, sound);
mp.start();
}
}
and in my main activity I call the method playSound for all of my buttons
button_name.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
gestoreSuoni.eseguiSuono(getApplicationContext(),R.raw.tieni_suono);
}
});
At first it works, but after 20-30 clicks on my buttons, I can't hear sound anymore and i get this message from LogCat: Mediaplayer error (- 19,0).
What does is mean?
Thanks
Upvotes: 0
Views: 141
Reputation: 4267
Following the example suggested by Ilya Demidov and the advice of Chinmoy Debnath I add this part of code to the method that manages the MediaPlayer
public class GestoreSuoni{
MediaPlayer mp;
public void playSound(Context context,int sound){
//new code/////////
if (mp!=null) {
mp.release();
mp = null;
}
///////////////////
mp = MediaPlayer.create(context, sound);
mp.start();
}
}
Now it seems to work. I hope I have solved (with your help) the problem correctly
Upvotes: 0
Reputation: 2824
Please release the memory when it stops. You are getting this error due to out of memory
as you are allocating memory every time
when you touch the button for playing again.
Upvotes: 2