问题
I encountered with a weird problem and hope that someone can answer it.. I have 4x4 buttons, each of them plays a short 1 second sound if I click on it. My code for it:
Button_1= (Button) findViewById(R.id.button1);
Button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sound!=null){
sound.stop();
sound.release();
sound=null;
}
sound = MediaPlayer.create(Main.this, R.raw.short_sound);
sound.start();
}
});
So what happens is that I touch all the buttons in row, so firstly the button1, then button 2 and every time I touch it, a short sound gets played. But sometimes when I reach the 14th or 15th or 16th button it doesnt play the sound it should. The other buttons work but somehow the last 1,2 or 3 sometimes doesnt play any sound. If I start touching the buttons backwards, so I touch the 16th button first it always works, but then maybe the 1st and 2nd button remain silent.
What might cause this? LogCat does not write anything.
回答1:
When you call MediaPlayer.create() you allocate resources that aren't necessarily released by the GC mechanism. You should manually release the MediaPlayer.release() routine. when you are finished with the object
来源:https://stackoverflow.com/questions/13035958/sound-sometimes-remains-silent-when-playing-more-sounds-in-a-row-why