1
votes

I have a view with four ToggleButtons, each playing a different sound at each state. So in total i have eight MediaPlayer instances. When the user selects one toggle button a sound will play, depending on what the value of the toggle button is. Here is my code for one ToggleButton.

MediaPlayer pos = MediaPlayer.create(Set.this, R.raw.pos)
MediaPlayer neg = MediaPlayer.create(Set.this, R.raw.neg);

private void onPlayer1Clicked() {
    if (((ToggleButton)Player1).isChecked())
     positive();
   else
     negative();
}

private void positive() {
    pos.start();
}

private void negative() {
    neg.start();
}

Where i have come stuck is when another ToggleButton is pressed before the MediaPlayer has stopped, the MediaPlayer plays both tunes until the fist one has finished. I could stop the MediaPlayer on the ToggleButton being pressed but i don't really want to type it out for every button as that would be seven tunes to Stop() at once.

Does anyone know how to stop all MediaPlayer instances with a simple command?

2
what kind of sound you have?? - moDev

2 Answers

1
votes

Use SoundPool . It fit better for this purpose. Check this link for details.

0
votes

Put all MediaPlayers in a Collection.

Then you can just iterate over the collection and stop them.