Aldy
Aldy

Reputation: 83

mediaplayer stop sound after 30 click?

hi I would like to ask why is that after clicking a button w/ sound 30 times on the 31 onwards the sound will no longer be heard and will hang after a few more click? can anyone please help me? thanks in advance here is my code.

SharedPreferences soundPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean sound = soundPrefs.getBoolean("Sound", true);
MediaPlayer clickLetters = MediaPlayer.create(Gameplay.this, R.raw.click_letters);

        switch(v.getId()) {
            case R.id.btnA:
                if (sound == true)
                {
                    clickLetters.start();
                }
                Answer = Answer + alphabetA;
                tvAns.setText(Answer);

            break;
            case R.id.btnB:
                if (sound == true)
                {
                    clickLetters.start();
                }
                Answer = Answer + alphabetS;
                tvAns.setText(Answer);
            break;

Upvotes: 0

Views: 289

Answers (1)

Simon
Simon

Reputation: 18113

probably because you are allocating a new MediaPlayer instance on each click. you should call release(), to free the resources, If not released, too many MediaPlayer instances will result in an exception. you should create only 1 instance of mediaplayer and reuse it as much as possible

Upvotes: 1

Related Questions