Skipping Clips

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!

One easy way is to create a pool of clips that you draw from to play the sound effects. That way you can for example play a particular sample multiple times simultaneously (if say 5 explosions happened at around the same time), and this fixes the issue of continuously stopping and restarting a single clip in order to play a particular sample in rapid succession. Your clip pool size is generally going to be around 32, since that is the standard maximum.

Ah. Yeah I was looking around the forum and it looked like you were the man to ask. I’ll take a look at that option. I hope I don’t need it. The game I’m working on is a Pokemon clone, and I consider those to be pretty much the ultimate closed system. Nothing new ever needs to be generated, so if there was ever a need for 5 explosions at once (like the move Explosion or Self Destruct, I just realized), there would probably be a sound effect just for it. But since the point of the game is to learn (and oh boy have I learned things), I’ll give your suggestion a shot. Thanks, man.

EDIT - Woo I figured it all out. Mods you can close this puppy up. The solution I used can be found here: http://www.dreamincode.net/forums/index.php?app=blog&module=display&section=blog&blogid=114&showentry=1631