File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes Problem with sound clip Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Problem with sound clip" Watch "Problem with sound clip" New topic

Problem with sound clip

Andy Joness

Joined: Sep 15, 2009
Posts: 24
I've made a game of breakout and now I'm adding the sounds. I'm getting the following error at runtime:

Exception in thread "Thread-18" java.lang.IllegalStateException: Clip is already open with format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian and frame lengh of 23079

This error happens each time the ball hits a brick and the brickhit.wav sound plays.

The code I've got so far in my sound class is:

Any ideas how to prevent this error? Cheers

ps For some reason the same thing doesn't occur when the ball hits the paddle...ONLY when it hits a brick
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

I think declaring clip as an instance variable is going to cause a problem when used with threads like that. It's possible for a sound clip to get opened and never stopped.

Think about this, you have two sound clips, A and B. You call playSound and pass A's info in as a parameter. A thread starts up, assigns A to the clip instance variable and starts playing A's sound. Then you call playSound again with B's info. A new thread starts up, assigns B to the clip instance variable (replacing A's reference, without ever calling stop on A's Clip) and starts playing B's sound.

If you call your stop method, B's Clip gets stopped, because it's referenced through the clip instance variable - but A's Clip never got stopped. I'm guessing that the next time you pass A's info into AudioSystem to create a Clip for it, it still has the open Clip that never got stopped and just returns that. Then you get an error because you're trying to re-open a Clip that hasn't been stopped.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Problem with sound clip
It's not a secret anymore!