title: Java - playing the same sound multiple times at once
body: <p>I'm developing a game and I'm having some issues playing the same sound multiple times, as in if you activate one sound that is already playing it shouldn't cancel the first one. The solution I found here on Stackoverflow was to read it into a byte array which I did the following way:</p>
  
  <pre><code>public SoundObject(AudioInputStream audioIn) {
  
      try {
          af = audioIn.getFormat();
          size = (int) (af.getFrameSize() * audioIn.getFrameLength());
          audio = new byte[size];
          info = new DataLine.Info(Clip.class, af, size);
          audioIn.read(audio, 0, size);
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public void playSound() {
      try {
          Clip localSound = (Clip) AudioSystem.getLine(info);
          localSound.open(af, audio, 0, size);
          localSound.start();
      } catch (LineUnavailableException e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>This worked perfectly up until now that I tried to export it in a jar file, the jar file for some reason has issues playing a byte array which leads to the sounds cutting off after just a few milliseconds, I managed to find one post with the same issue where someone suggested the following solution:</p>
  
  <pre><code>    public SoundObject(String filePath) {
      try {
          AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Loader.class.getResource(filePath));
          clip = AudioSystem.getClip();
          clip.open(audioInputStream);
      } catch (IOException e) {
          e.printStackTrace();
      } catch (LineUnavailableException e) {
          e.printStackTrace();
      } catch (UnsupportedAudioFileException e) {
          e.printStackTrace();
      }
  }
  
  // Play the sound in a separate thread.
  public void playSound() {
      Runnable soundPlayer = new Runnable() {
  
          @Override
          public void run() {
              try {
                  clip.setMicrosecondPosition(0);
                  clip.start();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      };
      new Thread(soundPlayer).start();
  }
  </code></pre>
  
  <p>Using the second method worked for jar files but the issue then is instead that the sound does not play as good as it did before, I have a shoot function and if you shoot very fast this method doesn't always pick up that and sometimes fails to play a sound when I press space to shoot. So with these 2 solutions I have to choose from either having unreliable sound that doesn't always play or I won't be able to export it to a runnable JAR.</p>
  
  <p>Does anyone have any experience with these kind of issues?</p>
  
  <p>edit: Here's a clip to show you how the 2 jars work: <a href="https://www.youtube.com/watch?v=wZPOIhHZSJM&amp;" rel="nofollow">https://www.youtube.com/watch?v=wZPOIhHZSJM&amp;</a></p>