The problem is that Clip already starting the daemon stream to play the wave file.
So, the flow of execution of your code is as follows:
- the main thread starts a secondary (useless) thread
- secondary stream starts the daemon stream (which will play the sound)
- at the same time, the main stream continues to print something, while the secondary stream exists
- When the secondary stream finishes launching the playback stream, it ends, so the secondary stream will no longer be in the active state
- the main thread will notice that the secondary thread is inactive and will also end.
- Since the playback thread is a daemon thread, the JVM will exit (because only the remaining threads are daemon threads).
The end result is exactly what you see: some text is printed by the main stream, while the secondary stream starts the play thread, and when the play thread starts playing, the arrow, the JVM ends. Sometimes you can even hear a βclickβ from the headphones (as the sound starts to play) before the JVM comes out.
The simplest fix is ββthat a secondary stream (i.e., a non-daemon stream) occurs during sound playback from outside.
... clip.open(ais); clip.loop(-1); Thread.sleep(amountToSleep); ...
It is important to note : about 1 year ago, when I was working with this Java API, I noticed that the getMicrosecondLength() method is an error. I encoded on both Windows and Linux, and on one platform I got the correct value, but on another, the same method would return the length in milliseconds!
I found that the most reliable way to get the actual length of the sound is to use the getFrameLength() method and calculate the length from it.
I could not find the code that I wrote then on this laptop. I will check later on another PC, and if I find it, I will post a complete example (which works reliably with both Windows with Sun JVM and Linux with OpenJDK or Sun).
source share