Need help to implement sound in my game

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.

just createing a new audio clip wont load the sounds. you have to call play on them(and then stop right after). but a better solution would be to look into the java sound api, its not that much more complicated.


public class SoundPlayer implements LineListener{
    private Clip clip;
    public SoundPlayer()  {
        clips = new ArrayList<Clip>();
        loadSounds();
    }
    public void playClip(){
        if(clip!=null){
            clip.start();
        }
    }

    public void loadSounds(){
        try{
            AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("sounds/shotsingle.wav"));
            AudioFormat format = stream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            clip = (Clip) AudioSystem.getLine(info);
            clip.addLineListener(this);
            clip.open(stream);
           
        }catch(UnsupportedAudioFileException e){

        }
        catch(IOException e){

        }catch(LineUnavailableException e){

        }
    }
    public void update(LineEvent event) {
        
        if (event.getType() == LineEvent.Type.STOP) {
            event.getLine().close();
        }
    }
}

exploring a bit the sound api and reading from a book I made this. however, the clip gets played once, and then when it needs to be replayed it does not play. how can I fix this?

In my games, I never close the line, and use this to trigger a clip :

clip.stop();
clip.setFramePosition(0);
clip.start();

This works fine as long as you don’t want the same clip played in parallel.

Lilian

now the clip gets played again if I ask it too, but sometimes it is played correctly and sometimes it isn’t. Any ideas?. To explain myself, sometimes I hear the full .wav that is 2 secs long and others I just hear a small beep and nothing more.