Java 不使用JAR文件播放音频

Java 不使用JAR文件播放音频,java,audio,jar,resources,Java,Audio,Jar,Resources,我创建了一个在netbeans IDE中播放音频的项目。这些音频文件被放在Classes文件夹中。 虽然当我将其创建为JAR文件时,它无法定位音频文件。我甚至将文件复制粘贴到新的dist文件夹中。 下面是一段代码: private void playSound39() { try { /**Sound player code from: http://alvinalexander.com/java/java-audio-example-java-au-play-sound

我创建了一个在netbeans IDE中播放音频的项目。这些音频文件被放在Classes文件夹中。 虽然当我将其创建为JAR文件时,它无法定位音频文件。我甚至将文件复制粘贴到新的dist文件夹中。 下面是一段代码:

    private void playSound39() 
{
  try
  {
/**Sound player code from:
http://alvinalexander.com/java/java-audio-example-java-au-play-sound    
*/      
    // the input stream portion of this recipe comes from a javaworld.com article.
    InputStream inputStream = getClass().getResourceAsStream("./beep39.wav");
    AudioStream audioStream = new AudioStream(inputStream);
    AudioPlayer.player.start(audioStream);
  }
  catch (Exception e)
  {
    JOptionPane.showMessageDialog(null,"Audio file not found!");
  }   
}

如果要将音频文件嵌入到程序中,必须将其放入包中的
src
文件夹中。
例如,我将演示一个用于设置按钮图标的代码(也适用于音频文件):
在创建JFrame时,我写道:

jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/PatientBig.png")));
我的项目中有一个名为
GUI
的包,其中有一个名为
Icons
的子包,其中有我的图标,它们都在
src
文件夹中。
当您使用
getClass().getResource
函数时,我更喜欢使用绝对路径。
在看到您的响应后,我注意到您一直在类路径的开头使用
,我复制了您发布的代码片段,并从路径的开头删除了
,并将我的音频文件
bark.wav
放在默认包的
src
文件夹中,它成功了

public class test {

    private void playSound39() {
        try {
            /**
             * Sound player code from:
             * http://alvinalexander.com/java/java-audio-example-java-au-play-sound
             */
            // the input stream portion of this recipe comes from a javaworld.com article.
            InputStream inputStream = getClass().getResourceAsStream("/bark.wav");
            AudioStream audioStream = new AudioStream(inputStream);
            AudioPlayer.player.start(audioStream);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Audio file not found!");
        }
    }
    public static void main(String[] args){
        new test().playSound39();
    }
}
然后我将音频文件放在一个名为
test1
的包中,并在
getResourceAsStream
函数中修改了路径,它再次工作:

public class test {

    private void playSound39() {
        try {
            /**
             * Sound player code from:
             * http://alvinalexander.com/java/java-audio-example-java-au-play-sound
             */
            // the input stream portion of this recipe comes from a javaworld.com article.
            InputStream inputStream = getClass().getResourceAsStream("/test1/bark.wav");
            AudioStream audioStream = new AudioStream(inputStream);
            AudioPlayer.player.start(audioStream);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Audio file not found!");
        }
    }
    public static void main(String[] args){
        new test().playSound39();
    }
}
最重要的是从路径中删除

试试这个

InputStream in = getClass().getResourceAsStream("/beep39.wav"); 

我认为您需要绕过InputStream的使用。运行getAudioInputStream方法时,使用InputStream作为参数会触发音频文件的可标记性和可重置性测试。音频文件通常无法通过这些测试。如果您使用URL或文件参数创建AudioInputStream,则可以绕过这些测试。我更喜欢URL,因为它看起来更健壮,可以“看到”罐子

然后,在while循环中,您将在AudioInputStream上执行read方法,并将数据发送到SourceDataLine

Java教程在他们的教程中介绍了这一点。此链接跳转到教程的中间


好的,Java 7 SDK中没有“AudioPlayer”。

尝试从cmd或终端运行jar以了解例外情况将音频文件放在项目的src目录中;我将音频粘贴到src文件夹中,并再次从projectproperties菜单创建了另一个JAR文件。尽管如此,JAR文件上的时间戳表明它是从今天早些时候开始的。还是不行,谢谢你,纳赛尔。删除
字符可以使其工作。我还将音频粘贴到src文件夹中。我忘了在前面单击“Clean and Build Project”,因为它没有更新jar文件。我不得不使用“/audio/filename.wav”而不是“src/audio/filename.wav”
URL url = getClass().getResource("./beep39.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);