Mmmm. I’m having trouble keeping track. The overlapping sounds are freezing the game? That makes me wonder about deadlocks. Your early code had a lot of synchronization. Are you still employing that? Infinite loops also come to mind as a plausible cause.
An earlier post said that the sounds would play but only half-way (“cut short”), and I didn’t see mention of the game freezing with that post. So I assumed that the game continued and only the sounds were acting weird. I have no idea what would cause that, except maybe attempting to use a given clip concurrently (where the second play causes the cut-off).
I had another thought about managing the flamethrower, if you are still entertaining various strategies for handling that. I am working from the assumption that the sound is a looping clip. The “problem” then is how to have the event which stops the flamethrower find this clip in order to issue a clip.stop(). Yes? I’m going to assume that there may be more than one flamethrower operating at a given time, but that we can identify the operator of the flamethrower when we start or stop it.
One thing I started doing is to create a single class called GameSound, and have it handle all the sound f/x. Within that class, I’d have a public method like .flameThrower(operator, flaming). Then have this method do the following:
if flaming is true, get an instance of the clip (from a preallocated pool), store it with the operator in a hash table, and start the clip.
if flaming is false, use the operator to locate the clip in the hash table, and stop the clip, resetting it to starting position and returning it to the pool.
I’ve been using the pooling idea for a ‘SoundField’ tool (used to randomly play a selection of sounds, with overlapping of same sounds supported) and also for various synths that are polyphonic (creating a pool of “notes”), and it has worked without needing synchronization to manage the pool. It took a little head-scratching to keep it concurrency safe, but it was doable.
What is kind of vexing for many is that these sorts of things aren’t available directly from Java or from most game libraries even though they seem like they should be common. Java sound gets pretty low level, and the high level capabilities they do provide are a take-em-or-leave-em proposition.