0

Whenever I open my .jar file, the sound will not work unless the files are in a folder titled "res" in the same directory as the .jar file. Now it will now work regardless of where the res folder is Here's my code

try {
         InputStream defaultSound = Game.class.getResourceAsStream("/TrailsGameMusic.wav");
         // getClass().getSy.getResource("/images/ads/WindowsNavigationStart.wav");

         System.out.println("defaultSound " + defaultSound);  // check the URL!
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
         Clip clip = AudioSystem.getClip();
         clip.open(audioInputStream);
         clip.loop(Clip.LOOP_CONTINUOUSLY);
    } catch (Exception ex) {
         ex.printStackTrace();
    }
GamerC42
  • 29
  • 7

1 Answers1

0

It's probably a problem with absolute path: If you declare it like that: "/res/TrailsGameMusic.wav", you are referring to absolute path - it works. If you will write only "TrailsGameMusic.wav", it will be a relative path - it will not work.

You can configure your resources using for example this link: How do I add a resources folder to my Java project in eclipse

Community
  • 1
  • 1
m.aibin
  • 3,528
  • 4
  • 28
  • 47