Playing wav files

Hey,

I found this wav file that is being used in another game. It’s a “shooting-sound” wav file, meaning that it plays once for everytime you shoot. It plays fine in the game it’s being used in, but when I try to use it in my game it, to play it everytime I shoot, then the sound seems to be crippled or something. Don’t know how to describe it better other than it plays smoothly in the other game, but if I shoot 2 shots in my game then the audio will somehow fail.

I’m using the spaceinvaders101 code to play the wave file.

Any ideas?

You’re going to want to use java.applet.AudioClip to play your sounds. I find it’s definitely the best way in Java2D. It’s fairly simple. Basically you create the AudioClip and then call one of three methods.

AudioClip shot = new AudioClip(“sounds/shot.wav”);
shot.play(); //Plays the sound once
shot.stop(); //stops playing the sound
shot.loop(); //keeps playing the sound until stopped

And that should work fine, very simply. It sounds like you may be getting problems because you have not preloaded the sound. Do so by making a SoundManager class that holds all your sounds.


import java.applet.AudioClip;
public class SoundManager
{
    private AudioClip[] sounds;
    public static final int SHOT_SND = 0;
    public static final int DIE_SND = 1;

    public SoundManager()
    {
        sounds = new AudioClip[2];
        sounds[SHOT_SND] = new AudioClip("sounds/shot.wav");
        sounds[DIE_SND] = new AudioClip("sounds/die.aif");
    }

    public void play(int sound)
    {
        sounds[sound].play();
    }

    public void stop(int sound)
    {
        sounds[sound].stop();
    }

    public void loop(int sound)
    {
        sounds[sound].loop();
    }
}

Understand? Make sense?

I’ll try that :slight_smile: thanks!