0

Hey I created a Soundboard with 200 Sounds but when I click a lot on the buttons the MediaPlayer stops working although I clean up the MediaPlayer.

Here is the code with only 2 Sounds:

public class MainActivity extends AppCompatActivity{
    public MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //plays sound
    public void sound1(View view){
        cleanUpMediaPlayer();
        mp = MediaPlayer.create(this, R.raw.sound1);
        mp.start();
    }

    public void sound2(View view){
        cleanUpMediaPlayer();
        mp = MediaPlayer.create(this, R.raw.sound2);
        mp.start();
    }

    public void cleanUpMediaPlayer(){
        if(mp != null) {
            if(mp.isPlaying()) {
                try {
                    mp.reset();
                    mp.prepare();
                    mp.stop();
                    mp.release();
                    mp = null;
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

What is wrong with this Code? And why does my MediaPlayer stops working after approximately 80 clicks.

Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
Aaron Waller
  • 175
  • 4
  • 21
  • Define "stops working" – Aaron Mar 07 '17 at 21:12
  • @Aaron the MediaPlayer stops playing a sound. – Aaron Waller Mar 07 '17 at 22:44
  • Cleaning up whitespace and indentation to make code a little more legible. – Geoffrey Wiseman Mar 08 '17 at 19:30
  • Are you still looking for an answer to this question? Maybe you have found it out yourself by now and can share what you've found? To me, it seems that this code is working well. I have copied it an run it and the music still plays well after 80 clicks. Maybe we should define "music". What sound files are you trying to play? – Zerato Oct 08 '17 at 20:31

0 Answers0