No sound after export to the bank

I have a problem with my application. When I run the application in Eclipse, the sound played well, but if I export the application to a runnable jar, the sound will not work.

The method in which the sound is played:

public static synchronized void playSound() 
    {
            new Thread(new Runnable() 
            {
                // The wrapper thread is unnecessary, unless it blocks on the
                // Clip finishing; see comments.
                public void run() 
                {
                    try
                    {
                        Clip clip = AudioSystem.getClip();
                        AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sound.wav"));
                        clip = AudioSystem.getClip();
                        clip.open(inputStream);
                        clip.start(); 
                    } 
                    catch (Exception e) 
                    {
                        System.err.println(e.getMessage());
                    }
                }
            }).start();
        }

Where is the mistake?

+5
source share
1 answer

The problem is this

AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sound.wav"));

The JAR file does not work getResourceAsStreamfor any reason. Therefore, I replace it with getResource:

AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));

and it works great.

+6
source

All Articles