• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Clip.start() behaviour

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't need Thread.sleep at all. Can you show us the code that creates and starts the clip?
 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is what I'm doing

 
Rob Spoor
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic