简体   繁体   中英

Mediaplayer stops playing after playing a few times

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);
         }
    });
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM