Michael
Michael

Reputation: 11

How do I loop threw my mediaPlayer on a delay without having sounds playing over one another?

I am trying to make an app that will tell the user "Ready Go" it will give the user two seconds to get set up and then a random amount of time after that it will say ready and a random amount of time after that it will say go. I got this part to work but now I want to loop it to where the user only has to press the button once and it'll keep playing. However, when I put it in a loop it'll run through it all at once and it ends up playing all the "Ready Go's" at the same time. Is there something I can do to prevent this?

public class MainActivity extends AppCompatActivity {
    public Context context;
    MediaPlayer mediaPlayer;
    MediaPlayer mediaPlayer1;

    public void start (View view) {
        play();
    }

    public void play() {
        //When I put a loop here I will get the audio playing over each other, I 
        //also tried to put it in the start method but I get the same result. Is 
        //there another way I should be going about this? Thanks for the help!

        long range = 2000L;
        Random r = new Random();
        long number = (long) (r.nextDouble() * range);
        long range1 = 1500L;
        Random rand = new Random();
        long number1 = (long) (r.nextDouble() * range);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("Info", "Ready");
                mediaPlayer.start();
            }    
        }, 2000 + number);
        Handler handler3 = new Handler();
        handler3.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("Info", "Go");
                mediaPlayer1.start();
            }
        }, 2200 + number + number1);
    }
}

Upvotes: 1

Views: 273

Answers (2)

Deˣ
Deˣ

Reputation: 4371

Just put this in onCreate method

mediaPlayer1.setOnCompletionListener(new OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer mp) {
        play();
      }
    });

This will call the play method again once it completes the mediaPlayer1. playing.

Upvotes: 0

Pradip Vadher
Pradip Vadher

Reputation: 186

Use CompletionListener instead of loop, First Call playAudio1 method in play(). code is below,

mediaPlayer1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            playAudio2();
        }
    });


    mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            playAudio1();
        }
    });

playAudio1 and playAudio2 methods,

private void playAudio2() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.i("Info", "Go");
            mediaPlayer1.start();
        }
    }, 2200 + number + number1);
}

private void playAudio1() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.i("Info", "Ready");
            mediaPlayer1.start();
        }
    }, 2000 + number);
}

Use mediaPlayer.prepare() or mediaPlayer.prepareAsync() before mediaPlayer.start().

Upvotes: 1

Related Questions