• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with sound clip

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic