title: UnsupportedAudioFileException with a .wav file
body: <p>I'm trying to load a .wav file in my EnviromentAudio object, but I received only an <code>UnsupportedAudioFileException</code> and I don't know why. Because the file is a wav and I've tried to encode it as an unsigned 8 bit, as a signed 16 bit, with a 44100 bit rate, as a GSM, as a A-law... long story short I've tried a lot of encoding, as many people suggested, but no one worked. Probably I'm not getting something, so, I want to ask what I'm doing wrong.</p>
  <p><em><strong>EDIT:</strong></em><br />
  As pointed out I should have specified some things: first of all, to set some context, I am using Java 8 to create a little pc game for a project, which must uses the basics components of java. Said that, I'm using the <code> ClassLoader</code>
  , because I have a mess in the project folder. It does not follow the convention and I have to keep like that. It's structured like this:</p>
  <pre><code>-src  
      -app
          -audio
              EnviromentAudio.java // Class that need to load soundtrack.wav
  -res  
      -audio
          Soundtrack.wav // Audio to be loaded
  </code></pre>
  <p>And I know that a <code>getResource..</code> should start always with a <code>/</code>, but if I add that slash, then every attempt to get a resource results in a NPE. Probably that's caused by the folders disposition and, by the way, the resources folder is set as source folder, so I'm not even quite sure about that, cause, also, I've already used the <code>getResource</code> to get other files without problems.</p>
  <p>In this case The <code>getResource</code> works fine, that is it retrieves the file, but the AudioSystem generates an error. I've tried to isolate the parties involved, but the only problem seems to be here. I'm adding the AudioManager class, the Audio class inherited by EnviromentAudio, and the whole EnviromentAudio, with the hope that it will be of help for a better understanding. I also provided a main in the AudioManager class, which should be enough to replicate the error.</p>
  <p>Audio class:</p>
  <pre><code>
  package application.core.audio;
  
  import java.util.ArrayList;
  
  import javax.sound.sampled.Clip;
  import javax.sound.sampled.FloatControl;
  import javax.sound.sampled.LineUnavailableException;
  import javax.swing.JOptionPane;
  
  public abstract class Audio
  {
      protected static final String AUDIOERROR=&quot;Error in loading audio. &quot;
              + &quot;Execution Failed, please, restart the game. &quot;
      protected static final String AUDIOERRORTITLE=&quot;Audio loading error&quot;;
  
      protected ArrayList&lt;Clip&gt; multimedia;
      protected Clip currentAudio;
  
      protected FloatControl gainControl;
  
      public Audio()  {
          multimedia=new ArrayList&lt;Clip&gt;();
          currentAudio=null;
      }
  
      protected abstract void getResources();
  
      public void playAudio(int index)    {
          try
          {
              currentAudio=multimedia.get(index);
              gainControl=(FloatControl) currentAudio.getControl(
                      FloatControl.Type.MASTER_GAIN);
              currentAudio.open();
          } catch (LineUnavailableException e)
          {
              e.printStackTrace();
              JOptionPane.showMessageDialog(null, AUDIOERROR,
                      AUDIOERRORTITLE, JOptionPane.ERROR_MESSAGE);
          }
          currentAudio.start();
      }
      public void loopAudio(int index)    {
          currentAudio=multimedia.get(index);
  //          gainControl=(FloatControl) currentAudio.getControl(
  //                  FloatControl.Type.MASTER_GAIN);
  //          currentAudio.open();
  //      currentAudio.start();
          currentAudio.loop(Clip.LOOP_CONTINUOUSLY);
      }
      public void repeatAudio(int index, int times)   {
          try
          {
              currentAudio=multimedia.get(index);
              gainControl=(FloatControl) currentAudio.getControl(
                      FloatControl.Type.MASTER_GAIN);
              currentAudio.open();
          } catch (LineUnavailableException e)
          {
              e.printStackTrace();
              JOptionPane.showMessageDialog(null, AUDIOERROR,
                      AUDIOERRORTITLE, JOptionPane.ERROR_MESSAGE);
          }
          currentAudio.loop(times);
      }
      public void stopAudio(int index)    {
          multimedia.get(index).stop();
          multimedia.get(index).close();
      }
  
      public void setVolume(float volume) {
          float range=gainControl.getMaximum()-gainControl.getMinimum();
          float gain=(range-volume)+gainControl.getMinimum();
          gainControl.setValue(gain);
      }
  
      public boolean currentAudioIsOpen() {return currentAudio.isOpen();}
      public void openCurrentAudio()  {
          if (!currentAudio.isOpen())
              try
              {
                  currentAudio.open();
              } catch (LineUnavailableException e)
              {
                  e.printStackTrace();
                  JOptionPane.showMessageDialog(null, AUDIOERROR,
                          AUDIOERRORTITLE, JOptionPane.ERROR_MESSAGE);
              }
      }
      public void openAndPlayCurrentAudio()   {
          if (!currentAudio.isOpen())
              openCurrentAudio();
          currentAudio.start();
      }
  
      public void playCurrentAudio()  {currentAudio.start();}
      public void loopCurrentAudio()  {currentAudio.loop(Clip.LOOP_CONTINUOUSLY);}
      public void repeatCurrentAudio(int times)   {currentAudio.loop(times);}
      public void stopCurrentAudio()  {currentAudio.stop();}
      public void stopAndCloseCurrentAudio()  {
          currentAudio.stop();
          currentAudio.close();
      }
  }
  
  </code></pre>
  <p>This is my EnviromentAudio class that produce the exception:</p>
  <pre class="lang-java prettyprint-override"><code>
  package application.core.audio;
  
  import java.io.File;
  import java.io.IOException;
  
  import javax.sound.sampled.AudioSystem;
  import javax.sound.sampled.FloatControl;
  import javax.sound.sampled.LineUnavailableException;
  import javax.sound.sampled.UnsupportedAudioFileException;
  
  public class EnviromentAudio extends Audio
  {
      public static final int SOUNDTRACK=0;
  
      public EnviromentAudio()
      {
          super();
          getResources();
          this.gainControl=(FloatControl) currentAudio.getControl(FloatControl.Type.MASTER_GAIN);
          }
  
      @Override
      protected void getResources()
      {
          try
          {
          ClassLoader loader=EnviromentAudio.class.getClassLoader();
              multimedia.add(AudioSystem.getClip());
              multimedia.get(SOUNDTRACK).open(AudioSystem.getAudioInputStream( // here the exception is thrown (on getAudioInputStream)
                      loader.getResourceAsStream(&quot;resources&quot;+File.separator+&quot;audio&quot;+File.separator+
                      &quot;soundtrack&quot;+File.separator+&quot;igpeSoundtrack.wav&quot;)));
              currentAudio=multimedia.get(SOUNDTRACK);
          } catch (LineUnavailableException e)
          {
              e.printStackTrace();
          } catch (IOException | UnsupportedAudioFileException e1)
          {
              e1.printStackTrace();
          }
      }
  
  }
  
  </code></pre>
  <p>AudioManager class:</p>
  <pre><code>
      package application.core.audio;
      
      public class AudioManager
      {
          private static AudioManager instance=null;
      
          private EnviromentAudio soundtrack;
          private PlayerAudio playerAudio;
      
          private AudioManager()  {
              soundtrack=new EnviromentAudio();
      //      playerAudio=new PlayerAudio();
      
              soundtrack.loopAudio(EnviromentAudio.SOUNDTRACK);
          }
      
          public static AudioManager getInstance()    {
              if (instance==null)
                  instance=new AudioManager();
              return instance;
          }
      
          public Audio getSoundtrack()    {return soundtrack;}
          public Audio getPlayerSounds()  {return playerAudio;}
      
          public void setVolume(float volume) {
              soundtrack.setVolume(volume);
              playerAudio.setVolume(volume);
          }
          public float getVolume()    {return soundtrack.gainControl.getValue();}
      
          public static void main(String[] args)
          {
              AudioManager a=AudioManager.getInstance();
          }
      
      }
  
  </code></pre>
  <p>And here is the error:</p>
  <pre><code>javax.sound.sampled.UnsupportedAudioFileException: Stream of unsupported format
      at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1020)
      at application.core.audio.EnviromentAudio.getResources(EnviromentAudio.java:29)
      at application.core.audio.EnviromentAudio.&lt;init&gt;(EnviromentAudio.java:18)
      at application.core.audio.AudioManager.&lt;init&gt;(AudioManager.java:11)
      at application.core.audio.AudioManager.getInstance(AudioManager.java:19)
      at application.MainApplication.audioInitialize(MainApplication.java:44)
      at application.MainApplication.main(MainApplication.java:25)
  </code></pre>