Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

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.
 
What? What, what, what? What what 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