0

I am trying to loop the sound in this code. In the finally block of the main try and catch I do this :

    if (loop) {
        auline.flush();
        run();
    } else {
        ended=true;
        auline.drain();
        auline.close();
    }

but it causes a stackoverflow. How can I safely loop this sound without creating a new instance of it?

Bart
  • 19,692
  • 7
  • 68
  • 77
Trixmix
  • 81
  • 9

1 Answers1

3

You're calling run from within run, this will eventually fill up the call stack & result in your stack overflow exception

Now, the question is, how do you overcome it?

You need to loop within the run method. The best way I can think of is to have a "exit" trigger in the run method

public void run() {
    while(loop) {
        //...play sound
    }
}

You could the use stop method to also trigger the loop flag

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    See also [`Clip.loop(Clip.LOOP_CONTINUOUSLY)`](http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html#loop%28int%29) as shown in the [Java Sound info. page](http://stackoverflow.com/tags/javasound/info). – Andrew Thompson Jul 29 '12 at 09:42