how to know when the tonegenerator has stopped playing

1.8k Views Asked by At

I kneed to know when my tone generator has stopped playing.

I want to play the dtmf tone for 50 ms and then wait 50 to play the next tone. i wanted to use this code.

private void playtones() {
    new Thread(new Runnable() {
        public void run() {
            for(int i=0 ; i<=19 ; i++){
                dtmftone.startTone(savetone.get(i) , 50);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }
            }

        }
      ).start();

}

The sleep starts before the tone has stopped playing. How can i check that the tone has stopped playing? Or is there at better way to do this?

Thx for the help.

1

There are 1 best solutions below

0
On

You should use the method wait(long millis) of ToneGenerator instead of Thread.sleep().
Be careful, the method does not wait the sound to have stopped, thus you have to take the duration of the beep into account:

int BEEP_DURATION = 50;
int WAITING_DURATION = 50;

...

for (int i = 0; i <= 19; i++) {
    dtmftone.startTone(savetone.get(i), BEEP_DURATION);
    synchronized(dtmftone) {
        dtmftone.wait(BEEP_DURATION + WAITING_DURATION);
    }
...

And I am not sure whether you want to wait 50ms between two beeps. If you don't, just delete the "+ WAITING_DURATION"