Java 导出到jar后没有声音
我的应用程序有问题。当我在Eclipse中运行应用程序时,声音播放得很好,但若我将应用程序导出到runnable jar,声音就不起作用了 方法,其中播放声音:Java 导出到jar后没有声音,java,eclipse,audio,embedded-resource,Java,Eclipse,Audio,Embedded Resource,我的应用程序有问题。当我在Eclipse中运行应用程序时,声音播放得很好,但若我将应用程序导出到runnable jar,声音就不起作用了 方法,其中播放声音: public static synchronized void playSound() { new Thread(new Runnable() { // The wrapper thread is unnecessary, unless it b
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();
}
哪里会出错 问题就在这里
AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("sound.wav"));
在JAR文件中,由于任何原因,getResourceAsStream无法工作。因此,我将其替换为getResource
:
AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("sound.wav"));
这很好。JAR中是否存在wave文件?我尝试解包JAR文件,发现wave文件在其中。哇,这太奇怪了