1

So I created a small app with a play and a stop button. When I press the play button it should loop until I press the stop button. The problem is, once I press the play button, I can't do anything else and the stop button is not responsive. If I try to press the stop button my app says app doesn't respond and quit. I don't know why this is happening and I'm super new to Android and Java so this is a bit complicated for me to know why it doesn't work. Here is my actual code :

Play and stop method :

   public void play() {
    playing = true;
    while (playing) {
        for (int i = 0; i < 4; i++) {
            if (buttonArray[i].isChecked()) {
                sp.play(snareId, 1, 1, 1, 0, 1);
            }
            if (!playing) {
                break;
            }
            try {
                Thread.sleep(1000);
                if(i == 3) i = -1;

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public void stop(){

    playing = false;//
}

Main activity

 playButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            mySample.play();
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            mySample.stop();
        }
    });
MMG
  • 3,226
  • 5
  • 16
  • 43
  • Have you checked the Exception e value while debugging? It should give you info related to the error. – Solx85 Mar 25 '20 at 09:16
  • The Exception e seems to give me those errors :"Thread[3,tid=10357,WaitingInMainSignalCatcherLoop,Thread*=0xee65dc00,peer=0x16000020,"Signal Catcher"]: reacting to signal 3" and :"Wrote stack traces to '[tombstoned]'" – Michel Pinto Mar 25 '20 at 09:27
  • https://stackoverflow.com/questions/9671546/asynctask-android-example – Solx85 Mar 25 '20 at 09:36

1 Answers1

1

According Android document, there are some common patterns to look for when diagnosing ANRs:

  1. The app is doing slow operations involving I/O on the main thread.
  2. The app is doing a long calculation on the main thread.
  3. The main thread is doing a synchronous binder call to another process, and that other process is taking a long time to return.

    And others.

You are in situation number 2 because you create an infinite loop while(playing) in main thread (it's UI thread in this case). Just listen to changes from check boxes and do your task respectively.

public void play(){
   //call startService to play sound
   //for each toggle button
   buttonArray[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if (isChecked) {
            //do your tasks
          } else {
            //do your tasks
          }
       }
   });
}

public void stop(){
    //call stopService to play sound
    //for each toogle button:
    buttonArray[i].setOnCheckedChangeListener(null);
}
TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • But I added a condition where I can manually stop the infinite loop isn't that enough?. How can I solve the problem while keeping my infinite loop? – Michel Pinto Mar 25 '20 at 09:25
  • Are buttonArray[i] checkboxes? – TaQuangTu Mar 25 '20 at 09:27
  • They are Toggle Buttons that are either checked or not. – Michel Pinto Mar 25 '20 at 09:28
  • Thanks for your help. So I tried your solution but the problem in your solution is that the sound is only played when I press the toggle buttons but my goal is to play the sounds when I press the play button only. It seems the play and stop button become irrelevant in your solution. – Michel Pinto Mar 25 '20 at 09:44
  • @MichelPinto for playing sound, you should use `Service`. https://stackoverflow.com/a/41222834/8531215 – TaQuangTu Mar 25 '20 at 09:49