No sound after export to jar

你说的曾经没有我的故事 提交于 2019-12-19 19:47:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16044136/no-sound-after-export-to-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!