Creating a new AudioClip from byte[]

Hi all!

I have a case where i load an .au file from a zip file. I have a wrapper class that i use for all sfx and which uses AudioClips. But it seems its only possible to load an Audioclip from an URL ???

I have access to the byte[] of the .au file and would like to stick using the AudioClip interface. Looking at the Applet.java implementation of getAudioclip got me nowhere, as it just forwards the call to AppletContext (which is defined per browser?)

try to have look at http://java.sun.com/j2se/1.4.2/docs/api/javax/sound/sampled/spi/AudioFileReader.html sicking it in an audioClip well… you can adleast play it.

url only is because applet security model and all its not expected to load stuff from many other places. Also what might be usefull to note is that some ppl might wonder why you are using zip’s while there is jar (profided offcourse that this concerns an applet in the first place offcourse).

You could try something like this, where your sound is in byte array musicLoop. 16bit samples are stored as byte pairs (big-endian) so you need to split them up suitably when storing them.

            AudioFormat audioFormat = new AudioFormat(16000f, 16, 1, true, true);
            DataLine.Info info = new DataLine.Info(Clip.class, audioFormat);
            clip = (Clip)AudioSystem.getLine(info);
            clip.open(audioFormat, musicLoop, 0, musicLoop.length);
            clip.loop(Clip.LOOP_CONTINUOUSLY);

I’m not sure whether using AudioSystem.getLine() is a good idea as I believe there is a bug in Java 1.5 to do with the mixer defaulting to a hardware mixer which doesn’t work. It may be necessary to enumerate all the mixers and explicitly choose the software mixer and then use that to do getLine().

If anyone has experience on this, please comment on whether the mixer issue is an issue with the above code.