1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class SoundPlayer { private AudioClip backgroundMusic; private AudioClip gunFire; private AudioClip hit; private AudioClip flak; private AudioClip mayday; private AudioClip explosion;
public SoundPlayer() { loadAllSounds(); }
private void loadAllSounds() { try{ backgroundMusic = Applet.newAudioClip(getClass().getResource("sounds/03 Crush.wav")); gunFire = Applet.newAudioClip(getClass().getResource("sounds/shotsingle.wav")); hit = Applet.newAudioClip(getClass().getResource("sounds/hit2.wav")); flak = Applet.newAudioClip(getClass().getResource("sounds/flak1.wav")); mayday = Applet.newAudioClip(getClass().getResource("sounds/mayday.wav")); explosion = Applet.newAudioClip(getClass().getResource("sounds/exp1.wav"));
}catch(Exception e){ } } |
My main game panel has a static instance of this soundPlayer called sp. and then the different classes in my game get the SoundPlayer by doing:
PacificFighterPanel.sp.playSound();
the problem is the sometimes the sounds are played, sometimes they are not, and when too many get played it sounds like crap.