• Post Reply Bookmark Topic Watch Topic
  • New Topic

New to audio. Clip not working.

 
Caleb Kemper
Greenhorn
Posts: 27
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my code.


I looked up a few examples and matched my code exactly. I even went ahead and stuck that print statement to see if the clip would even play, didn't print.
What's wrong? and why?
Thanks in advance.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:

1. You never associate your "input" to your clip. So the clip has no idea what it's supposed to be playing.
2. The Java sound API is asynchronous. Your application will end before it starts playing the sound. To demonstrate this, add the following after starting the clip:


(2.5). For some reason the clip.isRunning() call seems not to work if used immediately after clip.start(). I did the following and got it to print (I'm using a longer (27s) file).

My assumption is the thread that plays the sound might not be initialized, and the clip's "isRunning" flag conditions setup, if you call isRunning() too close to starting it.

Just a note about coding conventions: all classes should start with a CapitalLetterLikeThis.
 
Caleb Kemper
Greenhorn
Posts: 27
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, that did it. I read somewhere that the "clip.open(input)" part that I had in place was only for large files. Put that back in along with the sleep, and it works fine.
Thanks for the help!

EDIT: Also thanks for the coding convention reminder! I'd gotten lazy treating my class name as I would a typical variable.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help.
Just a note: That sleep call is a hack. If you're planning on developing an application you'll want to find a better way of preventing your application from exiting, but for demonstration purposes it works fine.
 
Caleb Kemper
Greenhorn
Posts: 27
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm actually going to be implementing it into a game I'm working on at the moment. Would it be considered an acceptable strategy if the Thread I'm stopping is one created specifically for the sound, leaving the main Thread running? What other strategies should I consider.... can't really think how I would implement a Timer, unless everything after the sound is on it. What are your thoughts?
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be considered an acceptable strategy if the Thread I'm stopping is one created specifically for the sound, leaving the main Thread running?

This is what's happening already within the Sound API. The code to play a sound will probably work something like this:

So it creates a dedicated thread just for playing the sound. When you play a sound it's roughly equivalent to saying to someone "go play a sound, and come back when you're done", then continuing on with what you're doing at the time.

I'm not sure why you're thinking you need to stop the sound thread? Are you thinking that your game processing needs to stop and wait for a sound to play?
The application will only exit if there are no non-background threads. The thread on which the sound is played is started as a background thread. So if you have another thread that's doing work and isn't a background thread, the application will continue running. When thinking about when the application will close, just ignore anything related to the Java Sound API.

From the perspective of thinking about when the application will exit: (this is your original code sample with any calls that execute asynchronously commented out)

If you look at the application from this point of view( instantiate some variables then go to end of method and exit ) it's easy to see why the Thread.sleep call is required to keep the sample application running.

 
Phil Freihofner
Ranch Hand
Posts: 126
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a cautionary note, I ran into a problem loading a wav file using the fileName with Java 7 users.



The commented out code was giving me a "Mark Reset" exception. The code using URL works fine with Java 7. This was for creating a SourceDataLine not a Clip, but the two are so similar I thought it might be worth a mention. It also works for audio resources packed in a jar or used by an Applet.
 
Caleb Kemper
Greenhorn
Posts: 27
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see what you mean now. I think I just blindly asked a question there. My game runs continuously anyway, so it shouldn't be a problem. Thanks for all the help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!