Sk1X1
Sk1X1

Reputation: 1373

No sound after export to jar

I have problem with my app. When I run app in Eclipse, sound played well, but if I export app to runnable jar, sound doesn't work.

Method, where 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 can be a mistake?

Upvotes: 6

Views: 4958

Answers (1)

Sk1X1
Sk1X1

Reputation: 1373

The problem is in this

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

in JAR file isn't working getResourceAsStream for any reason. So I replace it with getResource:

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

and this works fine.

Upvotes: 8

Related Questions