How to play 2 consecutive mp3 with MediaPlayer?

edited July 2018 in Android Mode

I am trying to play two consecutive mp3 (the second starting when the first ends). Is it possible?

At the moment I use a delay but this solution is far from being optimal. Thank you in advance

      act = this.getActivity();
      context = act.getApplicationContext();

      try {
        s1 = new MediaPlayer();
        s2 = new MediaPlayer();

        af1 = context.getAssets().openFd("sound1.mp3");
        af2 = context.getAssets().openFd("sound2.mp3");

        s1.setDataSource(af1.getFileDescriptor(), af1.getStartOffset(), af1.getLength());
        s2.setDataSource(af2.getFileDescriptor(), af2.getStartOffset(), af2.getLength());

        s1.prepare();// prepare the sound for playing/usage
        s2.prepare();

        s1.start();
        delay(800);  // <-------------- ugly!
        s1.release();
        s1 = null;

        s2.start();
        delay(600); // <---------------
        s2.release();
        s2 = null;
      }
      catch(IOException e) {
      }

Answers

  • Yes, that is not the proper way to do this. You need to look up the MeidaPlayer APi to see if it offer a callback when the file stops playing. Also feel free to explore previous entries with MediaPlayer keyword. Finally, please post in the new forum.

    Kf

Sign In or Register to comment.