I have the following code to play my sounds in a game:
protected void playSound(final String sound, final int playTime){
try {
sounds.get(sound).open();
}
catch(Exception e) {
System.out.println(e);
}
new Thread(new Runnable() {
public void run() {
Clip actualClip = sounds.get(sound);
actualClip.setFramePosition(0);
if(playTime < 0){
actualClip.loop(Clip.LOOP_CONTINUOUSLY);
}
else{
actualClip.loop(playTime - 1);
}
}
}).start();
}
The sounds are saved in an hashmap:
private HashMap<String, Clip> sounds;
When i play two different sounds "in the same time" (with a difference of 1 ms ;) ), they are playing parallel to each other ; so i can hear two sounds in the same time. Looks like this:
playSound("sound1", 1);
playSound("sound2", 1);
But when i try to play the same sound twice, it doesnt work:
playSound("sound1", 1);
//here its waiting in my programm
playSound("sound1", 1);
The thing is, i want to add an "death" sound - but two mobs can also die in the same time, or just one second before another. When this happens either nothing happens, or the sound just plays one time.
Why? I think im creating a new AudioClip of the same file, in an own thread? So why it isnt working?