mediaplayer stop sound after 30 click?

久未见 提交于 2019-12-11 20:28:35

问题


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;

回答1:


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



来源:https://stackoverflow.com/questions/20509397/mediaplayer-stop-sound-after-30-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!