title: Java Audio Stream (mp3spi lib), UnsupportedAudioFileException
body: <p>I have seen multiple Stack Overflow questions regarding streaming MP3 streams (like Icecast). They all say to use the MP3SPI libraries, which I am. MP3SPI is for allowing support to <code>audio/mpeg</code> mime types. That's what my Icecast stream is. I have all three jar files in my classpath correctly, but while using the same code they provide in an example, I still get <code>UnsupportedAudioFileException</code>:</p>
  
  <pre><code> javax.sound.sampled.UnsupportedAudioFileException: could not get audio input str
  eam from input URL
      at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:
  1153)
      at DJUtils.testPlay(DJUtils.java:16)
      at DJ.play(DJ.java:13)
      at DJ.init(DJ.java:4)
      at Loader.main(Loader.java:69)
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>public static void testPlay(){
      try {
          AudioInputStream in= AudioSystem.getAudioInputStream(new URL("http://localhost:8000/listen.m3u"));
          AudioInputStream din = null;
          AudioFormat baseFormat = in.getFormat();
          AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                  baseFormat.getSampleRate(),
                  16,
                  baseFormat.getChannels(),
                  baseFormat.getChannels() * 2,
                  baseFormat.getSampleRate(),
                  false);
          din = AudioSystem.getAudioInputStream(decodedFormat, in);
          // Play now.
          rawplay(decodedFormat, din);
          in.close();
      } catch (Exception e){
          e.printStackTrace();
      }
  }
  
  private static void rawplay(AudioFormat targetFormat, AudioInputStream din) throws LineUnavailableException, IOException{
      try{
          byte[] data = new byte[4096];
          SourceDataLine line = getLine(targetFormat);
          if (line != null)
          {
              // Start
              line.start();
              int nBytesRead = 0, nBytesWritten = 0;
              while (nBytesRead != -1)
              {
                  nBytesRead = din.read(data, 0, data.length);
                  if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
              }
              // Stop
              line.drain();
              line.stop();
              line.close();
              din.close();
          }
      }catch(IOException e){
          e.printStackTrace();
      }
  }
  
  private static SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
      try{
          SourceDataLine res = null;
          DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
          res = (SourceDataLine) AudioSystem.getLine(info);
          res.open(audioFormat);
          return res;
      }catch(LineUnavailableException e){
          e.printStackTrace();
          return null;
      }
  } 
  </code></pre>
  
  <p>My start script for this project:</p>
  
  <pre><code>java -Dfile.encoding=Cp1252 -classpath bin;lib/libs.jar;lib/graphics.jar;lib/mp3spi/mp3spi.jar;lib/mp3spi/jl.jar;lib/mp3spi/tritonus.jar; Loader
  </code></pre>
  
  <p>And my Icecast control panel says it's currently streaming <code>audio/mpeg</code>. I can access my stream fine by opening that URL in my code up in any media player. Can someone point out what I'm doing wrong? Thanks!</p>