Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 582
Sk1X1 Avatar asked Apr 16 '13 18:04

Sk1X1


People also ask

Does exporting produce sound?

Exporting Produces No Sound. Please help. Topic is solved POSTS HERE ARE PRIVATE. INSTRUCTIONS BELOW: ALL posts made in this forum are ' Private ' so that only you and staff can see them. This allows sharing of personal data, projects and other information.

Is it possible to export a WAV file without any addons?

Exporting in WAV format is possible without any addons. Exporting as MP3 requires that you install something called LAME encoder into Audacity. There is a way to get that kind of magic.

Can I export audio from a track Iname?

AME would not export audio from that track, but Premiere would. Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Why do my songs sound weird when they're in stereo?

If you have a stereo file with the left and right blue waves exactly out of step with each other, it may sound a little wacky, but files like that make it through quite a bit of quality control and nobody catches it. Until you play it in a mono sound system (one speaker) or mixdown to mono. Then the sound drops dead.


1 Answers

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.

like image 54
Sk1X1 Avatar answered Sep 21 '22 11:09

Sk1X1