Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with sound clip

 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic