Ahh, well I found a few examples and hacked away at it until I came up with this. I recommend you replace your AudioClip system with it (or a variation on it). Enjoy! ;D
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.sound.sampled.*;
public class SoundSystem
{
private javax.sound.sampled.Line.Info lineInfo;
private Vector afs;
private Vector sizes;
private Vector infos;
private Vector audios;
private int num = 0;
private HashMap map;
public SoundSystem()
{
afs = new Vector();
sizes = new Vector();
infos = new Vector();
audios = new Vector();
map = new HashMap();
}
public void addClip(String s)
{
try{
URL url = getClass().getResource(s);
//InputStream inputstream = url.openStream();
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(loadStream(url.openStream()));
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
audioInputStream.read(audio, 0, size);
afs.add(af);
sizes.add(new Integer(size));
infos.add(info);
audios.add(audio);
map.put(s, (Object)new Integer(num));
num++;
} catch(UnsupportedAudioFileException e) {
return;
} catch(IOException e) {
return;
}
}
public void addAllClips(final String ext) // EX: ".wav"
{
File dir = new File(".");
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
};
String[] children = dir.list(filter);
if (children != null)
{
for (int i = 0; i < children.length; i++)
{
addClip(children[i]);
}
}
}
private ByteArrayInputStream loadStream(InputStream inputstream) throws IOException
{
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte data[] = new byte[1024];
for(int i = inputstream.read(data); i != -1; i = inputstream.read(data))
bytearrayoutputstream.write(data, 0, i);
inputstream.close();
bytearrayoutputstream.close();
data = bytearrayoutputstream.toByteArray();
return new ByteArrayInputStream(data);
}
public void playSound(int x) throws UnsupportedAudioFileException, LineUnavailableException
{
if(x > num)
{
System.out.println("playSound: sample nr["+x+"] is not available");
}
else
{
Clip clip = (Clip) AudioSystem.getLine((DataLine.Info)infos.elementAt(x));
clip.open((AudioFormat)afs.elementAt(x), (byte[])audios.elementAt(x), 0, ((Integer)sizes.elementAt(x)).intValue());
clip.start();
}
}
public void loopSound(int x) throws UnsupportedAudioFileException, LineUnavailableException
{
if(x > num)
{
System.out.println("loopSound: sample nr["+x+"] is not available");
}
else
{
Clip clip = (Clip) AudioSystem.getLine((DataLine.Info)infos.elementAt(x));
clip.open((AudioFormat)afs.elementAt(x), (byte[])audios.elementAt(x), 0, ((Integer)sizes.elementAt(x)).intValue());
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
public void stopSound(int x) throws UnsupportedAudioFileException, LineUnavailableException
{
if(x > num)
{
System.out.println("stopSound: sample nr["+x+"] is not available");
}
else
{
Clip clip = (Clip) AudioSystem.getLine((DataLine.Info)infos.elementAt(x));
clip.open((AudioFormat)afs.elementAt(x), (byte[])audios.elementAt(x), 0, ((Integer)sizes.elementAt(x)).intValue());
clip.stop();
}
}
public void Play(String s)
{
try{
Object obj = map.get(s);
if(obj == null)
return;
int x = ((Integer)obj).intValue();
playSound(x);
} catch(UnsupportedAudioFileException e) {
return;
} catch(LineUnavailableException e) {
return;
}
}
public void Loop(String s)
{
try{
Object obj = map.get(s);
if(obj == null)
return;
int x = ((Integer)obj).intValue();
loopSound(x);
} catch(UnsupportedAudioFileException e) {
return;
} catch(LineUnavailableException e) {
return;
}
}
public void Stop(String s)
{
try{
Object obj = map.get(s);
if(obj == null)
return;
int x = ((Integer)obj).intValue();
stopSound(x);
} catch(UnsupportedAudioFileException e) {
return;
} catch(LineUnavailableException e) {
return;
}
}
}
EDIT: Added ‘addAllClips’.