title: How to read an audio file? Which method should I use?
body: <p>I have a panel with 2 buttons. When I click on the button 1, I'd simply like to read an audio file (a .WAV in that case). Then, when I click on the button 2, I'd like to stop the music.</p>
  
  <p>I do some research, but I'm a little confused about the different methods.
  Which one is the best in my case ? Can someone explains the difference between AudioClip, JavaSound and JavaMediaFramework please ?</p>
  
  <p>I've also try an example, but it contains errors.</p>
  
  <p>Here is my <strong>Main.class</strong> :</p>
  
  <pre><code>import java.io.ByteArrayInputStream;
  import java.io.InputStream;
  
  public class Main
  {
      public static void main(String[] args) 
      {
             SoundPlayer player = new SoundPlayer("C:/Documents and Settings/All Users/Documents/Ma musique/Échantillons de musique/Symphonie n° 9 de Beethoven (scherzo).wma");
             InputStream stream = new ByteArrayInputStream(player.getSamples()); 
             player.play(stream);
      }
  }
  </code></pre>
  
  <p>Here is my <strong>SoundPlayer.class</strong> :</p>
  
  <pre><code>import java.io.DataInputStream;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import javax.sound.sampled.*;
  
  public class SoundPlayer 
  {
      private AudioFormat format;
      private byte[] samples;
      /**
       * 
       * @param filename le lien vers le fichier song (URL ou absolute path)
       */
      public SoundPlayer(String filename)
      {
          try
          {
              AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename));
              format = stream.getFormat();
              samples = getSamples(stream);
          }
          catch (UnsupportedAudioFileException e){e.printStackTrace();}
          catch (IOException e){e.printStackTrace();}
      }
  
      public byte[] getSamples()
      {
          return samples;
      }
  
      public byte[] getSamples(AudioInputStream stream)
      {
          int length = (int)(stream.getFrameLength() * format.getFrameSize());
          byte[] samples = new byte[length];
          DataInputStream in = new DataInputStream(stream);
          try
          {
              in.readFully(samples);
          }
          catch (IOException e){e.printStackTrace();}
          return samples;
      }
  
      public void play(InputStream source)
      {
          int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10);
          byte[] buffer = new byte[bufferSize];
          SourceDataLine line;
          try
          {
              DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
              line = (SourceDataLine)AudioSystem.getLine(info);
              line.open(format, bufferSize);
          }
          catch (LineUnavailableException e)
          {
              e.printStackTrace();
              return;
          }
          line.start();
  
          try
          {
              int numBytesRead = 0;
              while (numBytesRead != -1)
              {
                  numBytesRead = source.read(buffer, 0, buffer.length);
                  if (numBytesRead != -1)
                      line.write(buffer, 0, numBytesRead);
              }
          }
          catch (IOException e){e.printStackTrace();}
  
          line.drain();
          line.close();
      }
  }
  </code></pre>
  
  <p><strong>LOGCAT</strong> :</p>
  
  <pre><code>javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
      at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
      at SoundPlayer.&lt;init&gt;(SoundPlayer.java:19)
      at Main.main(Main.java:8)
  Exception in thread "main" java.lang.NullPointerException
      at java.io.ByteArrayInputStream.&lt;init&gt;(Unknown Source)
      at Main.main(Main.java:9)
  </code></pre>
  
  <p>In advance, thanks a lot !</p>