Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从.jar文件播放声音_Java_Swing_Jar_Embedded Resource_Audioinputstream - Fatal编程技术网

Java 从.jar文件播放声音

Java 从.jar文件播放声音,java,swing,jar,embedded-resource,audioinputstream,Java,Swing,Jar,Embedded Resource,Audioinputstream,我看过无数不同的StackOverflow答案以及其他网站的答案,但没有一个解决方案解决了我的问题。我一辈子都不能播放我的.wav文件 这是我的密码: 声音等级: public class Sound { /** * Static file paths for each sound. */ public static String stepSound = "/resources/step.wav"; /** * Audio input stream for this sound. */

我看过无数不同的StackOverflow答案以及其他网站的答案,但没有一个解决方案解决了我的问题。我一辈子都不能播放我的.wav文件

这是我的密码:

声音等级:

public class Sound {
/**
 * Static file paths for each sound.
 */
public static String stepSound = "/resources/step.wav";

/**
 * Audio input stream for this sound.
 */
private AudioInputStream audioInputStream;

/**
 * Audio clip for this sound.
 */
private Clip clip;

/* -- Constructor -- */

/**
 * Creates a new sound at the specified file path.
 * 
 * @param   path    File path to sound file
 */
public Sound(String path) {
    // Get the audio from the file
    try {
        // Convert the file path string to a URL
        URL sound = getClass().getResource(path);
        System.out.println(sound);

        // Get audio input stream from the file
        audioInputStream = AudioSystem.getAudioInputStream(sound);

        // Get clip resource
        clip = AudioSystem.getClip();

        // Open clip from audio input stream
        clip.open(audioInputStream);
    } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
        e.printStackTrace();
    }
}

/* -- Method -- */

/**
 * Play the sound.
 */
public void play() {
    // Stop clip if it's already running
    if (clip.isRunning())
        stop();

    // Rewind clip to beginning
    clip.setFramePosition(0);

    // Play clip
    clip.start();
}

/**
 * Stop the sound.
 */
public void stop() {
    clip.stop();
}
}
导致错误的构造函数调用:

// Play step sound
new Sound(Sound.stepSound).play();
我知道这不是第一次在这个网站上被问到或回答这样的问题,但在这一点上,我已经尝试了几个小时的其他解决方案,我所发现的只是痛苦和挫折。如果需要,我可以发布更多代码。提前谢谢

编辑:我已经解包了.jar文件,并确认该文件确实存在。问题是URL最终为null,因此会抛出
NullPointerException


编辑#2:添加了更多代码以防出现其他问题。

从路径中删除
\resource
。@Yahya我想您的意思是更改行
公共静态字符串stepSound=“/resources/step.wav”
公共静态字符串stepSound=“/step.wav”
,这仍然会导致
NullPointerException
资源是项目中的一个包还是一个资源文件夹?以及什么
System.out.println(声音)打印?@Yahya你绝对不会相信,但是
URL
InputStream
或任何这些都没有问题。。。我把我的文件命名为
Step.wav
,在我的字符串文件路径中,我把
Step.wav
(注意小写的's')。一直以来,这只是一个大写错误。。。很抱歉浪费了你的时间。非常感谢你的帮助。这对我来说太尴尬了。