I've been trying to deal with sound on my Applet for a bit now, and Instead of trying all the different methods, what is the best way to play Sound in Java? There are a few requirements:
- Needs to be able to loop
- Needs to be able to load a WAV from an archive JAR(I think with the getClass().getResource)
- Needs to be able to play more than one sound at the same time, and not clip already playing sounds
Thanks you so much for looking at my question and I hope you guys have an answer!
Thanks to the wonderful help I almost have it working with this:
public class MyGame() {
Clip bullet;
public void init(){
try {
bullet = AudioSystem.getClip();
URL url2 = this.getClass().getResource("bulletSound.wav");
AudioInputStream ais2 = AudioSystem.getAudioInputStream( url2 );
bullet.open(ais2);
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
randomMethodToPlayBullet(){
bullet.setFramePosition(0);
bullet.start();
}
}
The problem is that the bullet sound plays, but if the randomMethodToPlayBullet is called say twice in a row, before the first bullet sound is done, the seonc one doesnt play.