Mediaplayer stops playing after playing a few time

2020-07-27 06:13发布

问题:

I've got a button that on press it plays a random sound clip followed by another and then another via one mediaplayer however after pressing the button numerous times (15-20 times) all audio just stops. I'm releasing the mediaplayer after the last audio clip is played so I don't see that being the reason. Any pointers?

public class Main extends Activity {


MediaPlayer mp;
Button generate;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    generate = (Button) findViewById(R.id.sound);

}

public void youSir (View view)
{
    generate.setVisibility(View.INVISIBLE);
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(3) + 1;
    switch (randomInt){
    case 1: mp = MediaPlayer.create(this, R.raw.beg); mp.start();
    break;
    case 2: mp = MediaPlayer.create(this, R.raw.begone); mp.start();
    break;
    case 3: mp = MediaPlayer.create(this, R.raw.begtwo); mp.start();
    break;
    }
    mp.setOnCompletionListener(new OnCompletionListener(){
        // @Override
        public void onCompletion(MediaPlayer arg0) {

            audioTwo();
         }
    });

}


protected void audioTwo() {
    // TODO Auto-generated method stub
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(3) + 1;
    switch (randomInt){
    case 1: mp = MediaPlayer.create(this, R.raw.start); mp.start();
    break;
    case 2: mp = MediaPlayer.create(this, R.raw.end); mp.start();
    break;
    case 3: mp = MediaPlayer.create(this, R.raw.mid); mp.start();
    break;
    }
    mp.setOnCompletionListener(new OnCompletionListener(){
        // @Override
        public void onCompletion(MediaPlayer arg0) {
            audioThree();
         }
    });
}

protected void audioThree() {
    // TODO Auto-generated method stub
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(3) + 1;
    switch (randomInt){
    case 1: mp = MediaPlayer.create(this, R.raw.three); mp.start();
    break;
    case 2: mp = MediaPlayer.create(this, R.raw.two); mp.start();
    break;
    case 3: mp = MediaPlayer.create(this, R.raw.one); mp.start();
    break;
    mp.setOnCompletionListener(new OnCompletionListener(){
        // @Override
        public void onCompletion(MediaPlayer arg0) {
            mp.stop();
            mp.release();
            generate.setVisibility(View.VISIBLE);
         }
    });
}

回答1:

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().