How to stop Media Player when you have more that one sound being played?

My app is a music pad, which allows the user to create their own music by pressing buttons that will play a specific sound, and if the users presses more than one button there will be more sounds being played at the same time.

I want to stop all sounds when the onPause() gets called. But I realized that if I have more than one sound being played by the same Media player Object it only stops the last sound that was attributed to it, and the others stay on playing.

It would be very unpleasant for the user to receive a call and having my app sounds still on playing :p

Any help would be appreciated.

@Override
protected void onPause() {
    super.onPause();

    try {
        while (mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
       }
    }catch (IllegalStateException e){

//Do nothing

      }

    }

EDIT

I tried to implement SoundPool API but since my app is very dynamic (users can upload their samples and also record using mic). And I have everything already implemented and working well, it would be a mess to change it now to another API rather than MediaPlayer. Is there any way to stop all sounds that were attributed to the MediaPlayer object and that are currently being played? maybe accessing to some other classes and using other methods to stop all the sounds?

Answers


I would suggest you look into the SoundPool API. It looks like that would be well suited for your task. It allows you to preload sound files and play them independently.

The SoundPool class manages and plays audio resources for applications.

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.


After a few research and trial and error there is no way that I discovered to stop all sounds being played by the same MediaPlayer Object, you can only control the last sound being attributed to it, all the other will continue until they end or the app gets destroyed.

Ok so what I did was:

I created an object for each key sound of my music pad, and Override the onPause() with the following code:

@Override
protected void onPause() {
    super.onPause();


    if (mAudioManager.isMusicActive()){
        for (MediaPlayer aMMediaPlayer : mMediaPlayer) {
            if (aMMediaPlayer != null) {
                try {
                    if (aMMediaPlayer.isPlaying()) {
                        aMMediaPlayer.stop();
                        aMMediaPlayer.release();
                    }
                } catch (IllegalStateException e) {

                    //Do nothing
                }
            }

        }
    }

}

Need Your Help

Using ASP.NET MVC how do I create a link for subscribing to an iCal from Outlook

asp.net-mvc outlook icalendar

I have a calendar in my ASP.NET MVC site that I want to publish so that it can be subscribed to by an Microsoft Outlook client. How can I accomplish this without manually specifying the whole url ...