title: Java - Multi threading sound clips to play at same time
body: <p>Problem;  Only hearing one sound clip when executed.  After one sound has played the other doesn't play &amp; neither can play at the same time.</p>
  
  <p>Result;  To be able to play 2 sounds at the same time.</p>
  
  <p>Code:   </p>
  
  <pre><code> import java.io.*;
   import javax.sound.sampled.*;
   public class ThreadPlay extends Thread {
   private String filename; // The name of the file to play
   private boolean finished; // A flag showing that the thread has finished
  
  private ThreadPlay(String fname) {
      filename = fname;
      finished = false;
  }
  
  public static void main(String[] args) {
      ThreadPlay s1 = new ThreadPlay("soundClip1.wav");
      ThreadPlay s2 = new ThreadPlay("soundClip2.wav");
      s1.start();
      s2.start();
  
      while (!s1.finished || !s2.finished);
  
  
      System.exit(0); // Java Sound bug fix...
  }
  
  public void run() {
  
      try {
          File file = new File(filename);
          AudioInputStream stream = AudioSystem.getAudioInputStream(file);
          AudioFormat format = stream.getFormat();
          DataLine.Info info = new DataLine.Info(Clip.class, format);
          Clip clip = (Clip)AudioSystem.getLine(info);
          clip.open(stream);
          clip.start();
          Thread.sleep(100);
          while (clip.isRunning()) { Thread.sleep(100); }
          clip.close();
      }
  
      catch (Exception e) { }
      finished = true;
  }
    }
  </code></pre>
  
  <p>Audio Lines:</p>
  
  <pre><code> AudioSystem.getMixerInfo() results in:
                    [Ljavax.sound.sampled.Mixer$Info;@52cc8049
  
   Array length: 7 
   Contents:
       PulseAudio Mixer, version 0.02
       default [default], version 4.4.0-66-generic
       PCH [plughw:0,0], version 4.4.0-66-generic
       PCH [plughw:0,1], version 4.4.0-66-generic
       PCH [plughw:0,3], version 4.4.0-66-generic
       PCH [plughw:0,7], version 4.4.0-66-generic
       Port PCH [hw:0], version 4.4.0-66-generic
  
    For each mixer, AudioSystem.getMixer(AudioSystem.getMixerInfo()[x])
     Results:
        org.classpath.icedtea.pulseaudio.PulseAudioMixer@685f4c2e
        com.sun.media.sound.DirectAudioDevice@7a07c5b4
        com.sun.media.sound.DirectAudioDevice@5ce65a89
        com.sun.media.sound.DirectAudioDevice@1de0aca6
        com.sun.media.sound.DirectAudioDevice@443b7951
        com.sun.media.sound.DirectAudioDevice@45283ce2
        com.sun.media.sound.PortMixer@4d76f3f8
  </code></pre>