Mediaplayer stops playing after playing a few times

不羁岁月 提交于 2019-12-04 18:46:31

This is a bit old question but I try to answer it in case someone else has a similar problem.

You are running out of audio resources. The code actually does not release all MediaPlayer instances. It releases only one out of three each run. audioTwo() and audioThree assign new MediaPlayer instance to the mp variable but they don't release the instance which is already stored in there. Do something like:

protected void audioTwo() {
    // RELEASING PREVIOUS MEDIAPLAYER
    mp.release();

    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(3) + 1;
    switch (randomInt){
    case 1: mp = MediaPlayer.create(this, R.raw.start); mp.start();
    break;
    ...

The same goes for audioThree().

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