0

I have problem with playing sounds in my java application. Sound is playing when user get message. Everything is working on OS X but on Windows it doesn't. I put file to jar using this command: jar uf client.jar getMsgSound.wav . I suppose that the problem is with find this file.

public String soundName = "getMsgSound.wav";

public void playSound()
{
try {
    File soundFile = new File(soundName);
    AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
    Clip clip = AudioSystem.getClip();
    clip.open(audioIn);
    clip.start();

    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }
}
Isnarf
  • 3
  • 2

1 Answers1

1

Even if it's on Mac OS, I don't think you can read a file from a jar this way. You might just happen to place the file somewhere in your project and it gets loaded.

I just tested with reading the file directly without putting it into a jar, it worked for both Mac OS and Windows.

So as Greg just pointed out in the comments, you should load it from the jar file using ClassLoader.

See this post for how to load resources using ClassLoader.

Community
  • 1
  • 1
lkq
  • 2,326
  • 1
  • 12
  • 22
  • You're right. I put on OS X file.wav with my jar, thats why it worked here. On Windows was only a jar file. That worked for me : public String soundName = "/getMsgSound.wav"; InputStream in = getClass().getResourceAsStream(soundName); AudioInputStream audioIn = AudioSystem.getAudioInputStream(in); I add file to jar with maven: src/resources getMsgSound.wav – Isnarf Mar 11 '16 at 15:38
  • Yeah, should always use Class Loader to load resource files. – lkq Mar 14 '16 at 01:38