i have a button that plays a sound.
When i push the button multiple times, it plays the sound multiple times.
This is oke, i want this.
But when i click the stop button it must stop all sounds currently playing.
I used:
while (mediaPlayer.isPlaying()){mediaPlayer.stop();}
but it is not working, the sounds keep on playing. Can someone help me?
Here is my full code:
public class HelloSoundboard extends Activity {
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button item1 = (Button)findViewById(R.id.item1);
item1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.atyourservice);
mediaPlayer.start();
}
});
Button stop = (Button)findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
while (mediaPlayer.isPlaying()){mediaPlayer.stop();}
// mediaPlayer.stop();
}
});
}
}
SoundPoolis a much better alternative for this purpose. I would caution strongly against instantiating multipleMediaPlayerinstances as most systems do not have the resources to generate many parallel active instances. You wil find on many device that hitting the button upwards of 5 times will cause a memory based crash.As far as stopping all active streams, there is not baked-in function for this, but it’s easy to accomplish in a manner to similar to your existing code. As a side note, there is an
autoPause()method, which halts all streams, but it doesn’t truly end their playback (as the method name insinuates). Here is a simple example to manage your audio streams:It is much more memory efficient to manage a list of streamID values than
MediaPlayerinstances, and your users will thank you. Also, note that it is safe to callSoundPool.stop()even if the streamID is no longer valid, so you don’t need to check for existing playback.HTH