This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Clip.start() behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Clip.start() behaviour" Watch "Clip.start() behaviour" New topic
Author

Clip.start() behaviour

Arka Sharma
Ranch Hand

Joined: Jun 15, 2011
Posts: 102

Hi,

I have written a very simple code to play .wav files using Java sound api.My question is this that why do I need to put Thread.sleep() to halt the execution after calling clip.start() method.The behaviour of startn is not clear to me actually.I have seen java docs for Clip as well as Sun's doc for Sound API.

Regards,
Arka
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You shouldn't need Thread.sleep at all. Can you show us the code that creates and starts the clip?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Arka Sharma
Ranch Hand

Joined: Jun 15, 2011
Posts: 102

This is what I'm doing

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Apparently calling start will create a daemon thread for playing the clip in the background without blocking until it's ready, and this daemon thread does not cause the JVM to stay alive. Therefore, when your main method ends, so does the clip. Also, when the loop hits the next iteration, the clip is overwritten, causing the existing clip to stop.

If you need to play music files and block until they are done, you should use either JMF or a third party library. JMF is event based so you'd need to write your own method to wait for the end. A quick approach:
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Clip.start() behaviour
 
Similar Threads
java beans and oo
JAR + Including Files
Get wav file reference from resource map
New to audio. Clip not working.
Java sound: how to play a file?