Fast way to play sounds in Java Applet

Saw a post slightly below this asking same question but didn’t find an answer so I’ll repost his question since I am having the same problem.

I am playing small 22khz 16 bit WAV sound clips in a Java Applet using the following code:


InputStream is = loadFile(filename);
AudioInputStream ain = AudioSystem.getAudioInputStream(is);
DataLine.Info info = new DataLine.Info(Clip.class, ain.getFormat());		
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(ain);
clip.start();
// I close the clip object with a linelistener once it's done playing

Works great, but takes about 6ms to execute, even on sounds that have already been loaded. I need to be able to play this sound more than once, and allow overlapping of that same sound.

Would like something more responsive - anyone have a faster method to play sound?

Some things I have tried:

  • Holding a reference to the clip instance rather than reinstantiating seems to drop from 6ms to 2ms, but then repeating the same sound interrupts itself… I could get around this by keeping say, 4 references to the same clip but I’m also having issues with sounds entirely cutting out (never playing again) using this method.
  • The basic play(getCodeBase(), filename) stuff is slow and doesn’t allow overlapping whatsoever
  • OGG code streaming in a separate thread, this seems decent for music but for sound clips not responsive enough

Thanks,

Jesse

Load the file in a byte[] and create a new ByteArrayInputStream for every play.

Been a while before I was able to try this out, but wanted to post thanks because this totally worked and solved the problem. Plays mega fast now

looking for a mor simple, faster and more stable way ? humm…


AudioClip a=this.getAudioClip();
a.play();

and you can overlapp same sound if you use toolkit.newAudioClip();