Search code examples
javaandroidandroid-mediaplayer

mediaplayer how to stop all sounds?


When I press the mute button on action bar that I have created it only stops the last sound being played and not all the sounds.
Also since the sound doesn't stop if I press the button(5-6 times) to play the sound and press mute on the same activity then go back and choose another activity and press that mute button the app crashes. Any ideas why?

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
public void pb1(View view) {
    mp = MediaPlayer.create(this, R.raw.sound1);
    mp.start();
  }
//inflates the menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menunot, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_sound:
            mp.stop();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mp.stop();
    mp.reset();
    mp.release();
    mp = null;

}

Solution

  • Just make an array list then stop all, Take a look at the example below:

    private static final int steps[] =
            {R.raw.step_1, R.raw.step_2, R.raw.step_3, R.raw.step_4, R.raw.step_5, R.raw.step_6, R.raw.step_7, R.raw.step_8, R.raw.step_9, R.raw.step_10
            };
    private int i = 0;
    private ArrayList<MediaPlayer> voices = new ArrayList<>(10);
    private Button btnPlay;
    private Button btnStop;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(i==10)i=0;
                final MediaPlayer voice = MediaPlayer.create(MainActivity.this, steps[i++]);
                voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        if (voice != null) {
                            voices.remove(voice);
                            voice.release();
                        }
                    }
                });
                voice.start();
                voices.add(voice);
            }
        });
        btnStop = (Button) findViewById(R.id.btnStop);
    
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int j = voices.size() - 1; j >= 0; j--) {
                    if (voices.get(j) != null) {
                        if (voices.get(j).isPlaying())
                            voices.get(j).stop();
                        voices.get(j).release();
                        voices.remove(j);
                    }
                }
            }
        });
    
    }