So I have created a very basic drum machine that should play snare sounds in a loop when I press play, only when the toggle buttons are on until I press stop. Here is the main code of the play() method :
public void play() {
playing = true;
while (playing) {
if (button1.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
if (!playing) {
break;
}
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (button2.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
if (!playing) {
break;
}
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (button3.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
if (!playing) {
break;
}
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (button4.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
if (!playing) {
break;
}
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
The problem is, when i press play, the app stop responding, i can't even press the stop button and i only here 1 snare sound even if i toggle more than 1 button and it doesn't seem to loop either since i only hear it once. So i'd like to know if my play() method is wrong and how can i fix the problem.
Thanks