Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

How to play 2 consecutive mp3 with MediaPlayer?

$
0
0

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

Viewing all articles
Browse latest Browse all 941

Trending Articles