I have a function playsounds()
which basically plays the same sound file again and again however with a different value of a float
called "balance" each time. After each play, I want the function to stop and listen for a user click on a different Button
. If there is no click the next play is called.
However, if the user clicks then I want to record that value of balance. My problem is that, once i start the function, there is no way to stop it in between. So even if I click, it gets unnoticed.
Could you help me in the problem ?
public void playsounds() throws InterruptedException
{
snd.setBalance(0);
snd.play(table);
Thread.sleep(2000);
tv.setText("Pressed at first call ");
snd.setBalance(0.04f);
snd.play(table);
Thread.sleep(2000);
tv.setText("Pressed at second call ");
snd.setBalance(0.08f);
snd.play(table);
Thread.sleep(2000);
tv.setText("and so on...");
snd.setBalance(0.12f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(0.16f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(1.0f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(1.84f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(1.88f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(1.92f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(1.96f);
snd.play(table);
Thread.sleep(2000);
snd.setBalance(2.0f);
snd.play(table);
Thread.sleep(2000);
}
If I understand your question correctly... could you use a boolean value and just check it after you are done sleeping?
public class MyClass implements OnClickListener{
private boolean buttonClick;
public void playSounds(){
//play sound
// sleep
if(buttonClick){
buttonClick = false;
//perform action or return
}
}
public void onClick(){
buttonClick = true;
}
}
Calling Thread.sleep() is probably not the best option if this is on the main thread, you will not be able to have instantaneous responses. You could also make the function smaller and call it over and over.. something like
public void playSound(float fValue){
snd.setBalance(fValue);
snd.play(table);
//sleep
}