Sound "Lag"

If I attempt to play 2 of the same sound around the same time (For example, if mario jumps through a large group of coins in a game), it causes some serious lag.

If you need an example of this, check out my platformer in the games section…

Is there any way to fix this? Or is it beyound the capabilities of JavaSound?

I’ve got a similar effect, however just until one sound is played for the very first time. When it plays the first time there’s a lag. Whenever the same sample is being played again no lag is noticeable.
So far I can live with this “very first time” lag. :slight_smile:

Hi everybody,
I’m beginning my experience with Java and I’m stuck with the same problem in repetition of sounds.

Have found any solution since?

I tried the following but I had no success:

  1. I created two versions of the same clip bang.wav (bang1.wav and bang2.wav) and loaded them in clipBang1 and clipBang2 via two different lines

  2. Every time a bullet is fired, I stop() the last played clip, set its frame position to 0 and start() the other clip.

Max

Well, if you’re using Clip you’re already using different lines for every Clip (and it’s a very bad implementation for sounds in a game).

A better method is to cache sounds in a byte array and use it to play them, instead of using streaming directly from disk.

Precaching sound is better becaure the other way you will always have lag, since your HD will have to fetch the file and play it. Usually sound FX (even if WAV) are really small (a bunch of KB), so you can have even hundreds of them in memory without reaching even a MB of RAM spent…

Well, there are two Clips. The usual one and the crappy ones from Applet. The usual ones are ok and play instantly (they are stored in ram as pcm), but you can get a slight delay at the first time you play one of them. As far as I can tell, it’s because there isn’t a mixer around and java needs to create a default mixer first.