making a delay in 'for' statement[JAVA]

I tried to make a code which makes random Numbers and play the melody based on its number. So I scripted like :

void playSample(int i) {

                switch(randomNum[i]) {  
                case 1:
                    //play do1.wav
                    se.setFile(sound1);
                    se.play();

                    break;

                case 2:
                    //re
                    se.setFile(sound2);
                    se.play();

                    break;

                case 3:
                    //mi
                    se.setFile(sound3);
                    se.play();

                    break;

                case 4:
                    //fa
                    se.setFile(sound4);
                    se.play();

                    break;

                case 5:
                    //sol
                    se.setFile(sound5);
                    se.play();

                    break;

                case 6:
                    //ra
                    se.setFile(sound6);
                    se.play();

                    break;

                case 7:
                    //ti
                    se.setFile(sound7);
                    se.play();

                    break;

                default:
                    //do2
                    se.setFile(sound8);
                    se.play();

                    break;

        }
}

and the actionListener for the button pressed

bListenMusic.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //plays the music.
                //playSample();
                for(int i=0; i<8; i++) {
                    playSample(i);
                    //waits.
                }
            }
        });
    }

I tried several methods include adding Thread.sleep(...); between for statement and the playSample method, but it makes the InterruptedException occurs. What can I do? +all the variables are working well.

1 answer

  • answered 2017-11-14 23:35 iDevRoids

    You Can use the Time Unit from concurrent util package for have pause .

    TimeUnit.SECONDS.sleep(1); // To sleep for one second or
    
    TimeUnit.MINUTES.sleep(1);  // To sleep for the Minute .
    
    
        import java.util.concurrent.TimeUnit;
    
    
    public class test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
            for(int i=0; i<8; i++) {
                playSample(i);
                //waits.
    
                    TimeUnit.SECONDS.sleep(3); // for Seconds
                    //TimeUnit.MINUTES.sleep(1); /// for minutes.
    
            }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    
    
    
    static void playSample(int i) {
    
        switch(i) {  
        case 1:
            //play do1.wav
            System.out.println("Sound1");
    
    
            break;
    
        case 2:
            //re
             System.out.println("Sound2");
            break;
    
        case 3:
            //mi
             System.out.println("Sound3");
            break;
    
        case 4:
            //fa
             System.out.println("Sound4");
            break;
    
        case 5:
            //sol
             System.out.println("Sound5");
            break;
    
        case 6:
            //ra
             System.out.println("Sound6");
            break;
    
        case 7:
            //ti
             System.out.println("Sound7");
            break;
    
        default:
            //do2
             System.out.println("Sound8 Default");
            break;}    }}