I'm using the following code to play sound effects in a game of mine:
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(sound).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
This is giving a heap out of space error and linking back to this code. I know I need to do clip.close()
, but if I do it right after clip.start()
the sound will die instantly and I can't delay since I'm in the main thread. Is there a way to do this without making a new thread for each instance of a sound effect?
Thanks