• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor MediaPlayer dont work after play sound alot times

avichay

Newbie
Jan 7, 2018
13
1
Hi Thx for looking...
I tried to play random sound by clicking & using thid method. It work preety fine but when i'm clicking alots of times on the button that make it all work, it stop plaing sound. plese help me to fix this problem.

Java:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random rnd = new Random();
                int number = rnd.nextInt(8)+1;
                    songText(number);
                    playSong(number);
            }


Java:
public void playSong (int number){

                if (mediaPlayer == null)
                    ;
                else
                    mediaPlayer.stop();


                switch (number){
                    case 1:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.hoken);

                        break;
                    case 2:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.lo);

                        break;
                    case 3:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.shalshove);

                        break;
                    case 4:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.barure);

                        break;
                    case 5:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.olay);

                        break;
                    case 6:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.lohoshevet);

                        break;
                    case 7:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.sogshell);

                        break;
                    case 8:
                        mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.ken);

                        break;
                    case 9:

                        break;
                    case 10:

                        break;
                }
               
                mediaPlayer.start();

            }
 
Upvote 0
Hi,

So, that's not really how MediaPlayer is designed to work, and is why LV426 suggests calling .stop() and .release().

There are two ways to fix this, and it mostly depends on the type of sounds you're actually playing.

If you're playing a number of short sounds, like sound effects, I recommend switching to using SoundPool: https://developer.android.com/reference/android/media/SoundPool.html With this you load the sounds and simply play them by an ID.

If they're longer (and larger), then I would recommend using a single MediaPlayer and using .reset() and then .load() to change what it's playing, rather than creating a new one each time.

Alternatively, if they're long (large) songs, and you'd rather not mess with that, you could try using ExoPlayer, potentially with a Timeline, which would allow you to "load" all the songs once, and then, in each of the cases of the switch, you'd set the "window" to whichever song you wanted to play. (And then call .pause() after each song ends, if you don't want it automatically playing the next one ;)
 
  • Like
Reactions: avichay
Upvote 0
Hi,

So, that's not really how MediaPlayer is designed to work, and is why LV426 suggests calling .stop() and .release().

There are two ways to fix this, and it mostly depends on the type of sounds you're actually playing.

If you're playing a number of short sounds, like sound effects, I recommend switching to using SoundPool: https://developer.android.com/reference/android/media/SoundPool.html With this you load the sounds and simply play them by an ID.

If they're longer (and larger), then I would recommend using a single MediaPlayer and using .reset() and then .load() to change what it's playing, rather than creating a new one each time.

Alternatively, if they're long (large) songs, and you'd rather not mess with that, you could try using ExoPlayer, potentially with a Timeline, which would allow you to "load" all the songs once, and then, in each of the cases of the switch, you'd set the "window" to whichever song you wanted to play. (And then call .pause() after each song ends, if you don't want it automatically playing the next one ;)

Welcome to AF. Good suggestion using SoundPool, you're right in saying that MediaPlayer isn't the right tool to use here.
 
  • Like
Reactions: avichay
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones