Search code examples
androidmultithreadingpostdelayed

How to delay an action inside a for loop before continuing the next loop over again


Hi I am new in android programming. My problem is I want to make a delay before continuing the next loop over and over again depending on the size of my Array so that the sounds will not overlap.

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


        }else if (u.equals("b4")){
            mp = MediaPlayer.create(this, R.raw.b4);
            mp.start();


        }else if (u.equals("c4")){
            mp = MediaPlayer.create(this, R.raw.c4);
            mp.start();


        }
    }
}

I tried using the Thread.sleep but I need to stop playing the sounds using a button click but i cannot click a button because of Thread.sleep so I have to disregard using it. But here is my code for that in case anyone have a solution for that. I've already tried the Thread.currentThread.interrupt but my sounds are overlapping again when i use that.

Code using Thread.sleep:

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();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block

            }

        }else if (u.equals("b4")){
            mp = MediaPlayer.create(this, R.raw.b4);
            mp.start();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block

            }

        }else if (u.equals("c4")){
            mp = MediaPlayer.create(this, R.raw.c4);
            mp.start();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block

            }

        }
    }
}

Another way that I tried is the postDelayed but it seems like it is not Delaying after the first loop.

Here is my code for postDelayed:

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

            handler.postDelayed(new Runnable(){
               @Override
               public void run() {  
                   mp.start();
               }            
            },4000);

        }else if (u.equals("b4")){
            mp = MediaPlayer.create(this, R.raw.b4);

            handler.postDelayed(new Runnable(){
               @Override
               public void run() {  
                   mp.start();
               }            
            },4000);

        }else if (u.equals("c4")){
            mp = MediaPlayer.create(this, R.raw.c4);

            handler.postDelayed(new Runnable(){
               @Override
               public void run() {  
                   mp.start();
               }            
            },4000);                
        }
     }
  }

I also tried changing ,4000 to (i+1)* 4000 as what others suggested but still its not working. And I also Tried Putting the Delay Outside the if else statements but the Result is just the same.

Is there anyone who can suggest and help me with my problem? Please. I've been working with for a week. Thanks in advance.


Solution

  • Try this code:

     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();
    
    
                    }else if (u.equals("b4")){
                        MediaPlayer mp1 = MediaPlayer.create(this, R.raw.b4);
                        mp.setNextMediaPlayer(mp1);
    
    
                    }else if (u.equals("c4")){
                        MediaPlayer mp2= MediaPlayer.create(this, R.raw.c4);
                        mp.setNextMediaPlayer(mp2);
    
    
                    }
                }