Hi all. It’s my first time working with sound in Java and I feel like I’m back in high school trying to figure out a monster. I wanted to create sound effects for my game. I started with this class, that seemed very helpful at first:
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
/**
* This enum encapsulates all the sound effects of a game, so as to separate the sound playing
* codes from the game codes.
* 1. Define all your sound effect names and the associated wave file.
* 2. To play a specific sound, simply invoke SoundEffect.SOUND_NAME.play().
* 3. You might optionally invoke the static method SoundEffect.init() to pre-load all the
* sound files, so that the play is not paused while loading the file for the first time.
* 4. You can use the static variable SoundEffect.volume to mute the sound.
*/
public enum SoundEffect {
EXPLODE("explode.wav"), // explosion
GONG("gong.wav"), // gong
SHOOT("shoot.wav"); // bullet
// Nested class for specifying volume
public static enum Volume {
MUTE, LOW, MEDIUM, HIGH
}
public static Volume volume = Volume.LOW;
// Each sound effect has its own clip, loaded with its own sound file.
private Clip clip;
// Constructor to construct each element of the enum with its own sound file.
SoundEffect(String soundFileName) {
try {
// Use URL (instead of File) to read from disk and JAR.
URL url = this.getClass().getClassLoader().getResource(soundFileName);
// Set up an audio input stream piped from the sound file.
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
// Get a clip resource.
clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
// Play or Re-play the sound effect from the beginning, by rewinding.
public void play() {
if (volume != Volume.MUTE) {
if (clip.isRunning())
clip.stop(); // Stop the player if it is still running
clip.setFramePosition(0); // rewind to the beginning
clip.start(); // Start playing
}
}
// Optional static method to pre-load all the sound files.
static void init() {
values(); // calls the constructor for all the elements
}
}
It does work pretty well. But let’s take one specific case. When I walk into a wall it’s supposed to play a sound. That works, but if I hold the key down, sometimes the sound does not play. Seems like something is getting overwhelmed playing the sound over and over. It’s not a total deal breaker for using this code, but I just wanted to know if there’s any way to fix it. Also the sounds in my game are very simple, and only come from one basic direction, so I don’t think I should have to use anything fancy like JOAL. However I could be wrong so let me know?
EDIT - That is not my code. I simply used it as a jumping off point. It is relatively unchanged in my game, aside from the constructor calls up top.
EDIT 2 - Well I figured out how to get past my problem for now, in a way I like, but I’d still like to know if there is anything I can do in the future if I need to rapidly play sounds. Thanks!