0

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

Razneesh
  • 1,147
  • 3
  • 13
  • 29

1 Answers1

1

The problem is all of your Thread.sleep calls. Each one of those blocks the UI thread for that period of time, freezing your application.

You should instead use Handler.postDelayed to do this without blocking the UI thread.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • Thank you very much for your answer. I think I understand my mistake so i tried to use your advice but I still have the same problem as before even when using Handler.postDelayed. Here is how I did it : `new Handler().postDelayed(new Runnable() { @Override public void run() { if (button2.isChecked()) { sp.play(snareId, 10, 10, 1, 0, 1); } } }, 1000);` Am I doing it wrong? – Michel Pinto Mar 18 '20 at 15:12