I am writing Ukelele Simulator, and I want to play a note on each string with a short delay in between. Currently, I have a method that returns an AudioClip containing the sound of the currently pressed note on each string, and I am attempting to play those AudioClips in succession with a 500 millisecond delay using Thread.sleep().
public static void main(String[] args){
//test it
UkeString stringG = new UkeString('G');
UkeString stringC = new UkeString('C');
UkeString stringE = new UkeString('E');
UkeString stringA = new UkeString('A');
try{
AudioClip G = stringG.getNoteAudio();
AudioClip C = stringC.getNoteAudio();
AudioClip E = stringE.getNoteAudio();
AudioClip A = stringA.getNoteAudio();
G.play();
Thread.sleep(500);
C.play();
Thread.sleep(500);
E.play();
Thread.sleep(500);
A.play();
Thread.sleep(500);
}
catch(Exception e){
System.out.println(e);
}
return;
}
However, I am encountering some odd irregular behavior from Thread.sleep(). Normally, when I execute the above code the first Thread.sleep() following G.play() either does not execute for the full 500 milliseconds or not at all, because the 'G' and 'C' notes are played in rapid succession. After that, the 'E' and 'A' notes will play as expected in 500 millisecond intervals.
The really weird thing is that if I run the program again before the other instance has ended (i.e. the notes in the old instance are still playing), then the new instance executes as expected with all the notes playing in 500 millisecond intervals. What's going on here?