ALD
ALD

Reputation: 23

handler.postDelayed is not working(android eclipse)

I am trying to make a delay after playing the set of sounds in mediaplayer so that it won't overlap. i have tried thread.sleep and it worked but the problem is I have a button for sound to stop but because of the thread.sleep, it is not working so i searched and tried for an alternative way in which i can make a delay without locking the UI and i came up with handler.postDelayed but it is not working. is there anyone who can help me solve this problem? thanks in advance.

here is my code:

protected void managerOfSound() {
    int size = tempq.size();
    for (int i = 0; i < tempq.size(); i++) {
        String u =tempq.get(i);

    //WHOLE
        if (u.equals("a4")){

            mp = MediaPlayer.create(this, R.raw.a4);
            mp.start();
            final Runnable r = new Runnable() {
                public void run() {

                    handler.postDelayed(this, 2000);
                }
            };

Upvotes: 1

Views: 6744

Answers (1)

Rick Sanchez
Rick Sanchez

Reputation: 4756

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    // stuff you wanna delay
  }
}, 2000);  

The postDelayed(Runnable r, long delayMillis ) causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached ( int htis case the UI thread) . The time-base is uptimeMillis(). Time spent in deep sleep will add an additional delay to execution.

Reference : Android API documentation

Upvotes: 1

Related Questions