简体   繁体   English

导出的jar文件不播放声音

[英]Exported jar file doesn't play sound

I made this method to play audio in my class and it works fine. 我使用这种方法在我的班级播放音频,它工作正常。 but for some reason when i export it to a jar file nothing happens. 但出于某种原因,当我将它导出到jar文件时没有任何反应。 I tried other solutions but i just get null-pointer exceptions. 我尝试了其他解决方案,但我只是得到空指针异常。 does anyone have an idea what i'm doing wrong 有没有人知道我做错了什么

public void welcome(){
        try { 
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sound/garage1.wav").getAbsoluteFile());
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
        } catch(Exception ex) {
            System.out.println("Error with playing sound.");
            ex.printStackTrace();
        }
 }

if you load the file like this (not as ressource of a class), the folder "sound" containing the "garage1.wav" has to be located in the working directory when you execute the jar, because new File("") points to the working directory. 如果您加载这样的文件(而不是类的ressource),当您执行jar时,包含“garage1.wav”的文件夹“sound”必须位于工作目录中,因为new File("")指向到工作目录。 For example: if you execute the jar with a double click on it, the folder "sound" must have the same parent folder as the jar. 例如:如果您通过双击执行jar,则文件夹“sound”必须与jar具有相同的父文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM