Are short Clips safe to use now?

Hello. I was reading through Killer Game Programming In Java and the writer mentions a bug which stops Clips from playing if they are under one second. On both my computers Clips that are under 1 second are playing fine. In fact they play better because as the sound will be played again and again rapidly it messes up when I load a clip longer than one second. Will this be fine on most computers now or am I still best to make all my clips over 1 second long?

The “short sound bug” is so fixed, they fixed it at least three times!

Seriously, it’s something that’s had a lot of regressions, and though it does appear to be behaving itself in 1.6, you never know when or if it’s going to bite you again. JavaSound has lots of issues, and serious apps tend to use third party libraries instead, like OpenAL (jogl and lwjgl have bindings for it), Paul Lamb’s sound library, or an FMOD binding of some flavor.

Thanks for the reply.

Yes I’ve come to hate java sound with a passion. I will look into other libraries for my next project. But I want to keep it simple for now. I’m just trying to finish my first applet and put it in the showcase section. As long at works ok for now on most computers, I’m ok with it.

I was googling more questions about java sound and this thread came up so I thought I may as well post here again!

I emailed my game to my friend for testing and everything works fine, except one of the sounds is inconsistent. It’s a puzzle game and you move tiles. You can move these tiles very rapidly, and it makes a sound, and this is the sound that is inconsistent. It works perfectly on both my computers though so it’s hard to tell what I can do to improve.

Would you say using a DataLine will be more reliable? I’ve not had much look with them before but that was for long sounds. I really want to stick to the standard library for this game. I was also thinking of just loading the sound twice and putting it in two clips and alternating them.

In what way “inconsistent”?

How is chaining two Clips different from looping a single Clip?

I have done this (two overlapping Clips and two overlapping SourceDataLines), but the motivation was that I wanted them to slightly overlap. Both methods work but are subject to the real-time precision constraints of Java audio (possible disruptions being things like garbage collection, unpredictable thread switching, bouncing back and forth from byte code interpretation to memory). So whether that would work will depend on the tolerances of the sound that you want to be heard.

For example, if the sound that needs to be continuous can variably overlap at the ends for a reasonable range of intervals (if I remember correctly, something like a 1/3rd or 1/2 of a second would be safe) and still sound acceptable, the alternation of two sounds should be okay.

For more consistent or precise timing, Riven had a very good suggestion, that you keep track of desired frame positions (which correspond to specific milliseconds via a calculation using the sample rate) and insert correct amounts of “silence” in order to add the sound at the exact required frame. This would be done with a continuous DataLine (continuous over the duration that you want to be precise about the timing).

By inconsitent he meant that the sounds would play sometimes but not others.

I had the same problem at first until I shortened the wav file so there was no silence at the end, then it worked fine on my computers. Althought it didn’t on my friends. This was the method I was using. Would it have made a difference if I called stop() before setting the framePosition to 0?

   
 public void playClip(String clipName) {
        Clip clip = soundsMap.get(clipName);
       
        if (clip != null) {
            clip.setFramePosition(0);
            clip.start();
        }
    }

This is the method I use now, just for the clicking sound that is played rapidly.


  public void click() {
        Clip clip = clicks.get(clickIndex);
        clip.setFramePosition(0);
        clip.start();
        clickIndex++; 
       
        if (clickIndex >= clicks.size()) {
            clickIndex = 0;
        }
    }

So I have four clicking sounds in an array. This apparently works fine on his computer. No one who tested my game in the showcase section had any problems either. I don’t know why this would work better than the other way.

I thought I would just add my solution to the problem in case in future someone else is searching.

I added another sound effect which at times needed to play quickly. And I noticed that if the sound was playing it would not replay despite restarting the frame position. But this code seems to work. I have to stop the clip and flush() it. Also drain() seems to work too. If I only flushed it if isActive() returned true, it still didn’t work sometimes, so I have to call flush every time.

 public void playClip(String clipName) {
        Clip clip = soundsMap.get(clipName);
       
        if (clip != null) {
             clip.stop();
             clip.flush();
             clip.setFramePosition(0);
             clip.start();
        }
    }