【问题标题】:Audio not playing with JAR File音频不与 JAR 文件一起播放
【发布时间】:2015-03-10 16:38:04
【问题描述】:

我创建了一个在 netbeans IDE 中播放音频的项目。这些音频文件被放置在 Classes 文件夹中。 虽然当我将它创建为 JAR 文件时,它无法找到音频文件。我什至将文件复制并粘贴到新的 dist 文件夹中。 这是一段sn-p代码:

    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!");
  }   
}

【问题讨论】:

  • 尝试从 cmd 或终端运行 jar 以了解异常
  • 把音频文件放到你项目的src目录下;

标签: java audio jar resources


【解决方案1】:

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

jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Icon/PatientBig.png")));

我的项目中有一个名为 GUI 的包和一个名为 Icons 的子包,其中存在我的图标并且它们都在 src 文件夹中。
当您使用getClass().getResource 函数时,我更喜欢使用绝对路径。
看到您的回复后,我注意到您在课程路径的开头继续使用.,我复制了您发布的sn-p,并从路径的开头删除了.,并放置了我的音频文件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();
    }
}

最重要的是从路径中删除.

【讨论】:

  • 我将音频粘贴到 src 文件夹中,并再次从 Project Properties 菜单中创建了另一个 JAR 文件。虽然,JAR 文件上的时间戳表明它是从今天早些时候开始的。还是不行。
  • 谢谢你,纳赛尔。删除 . 字符使其工作。我还将音频粘贴到 src 文件夹中。我之前忘记点击“清理和构建项目”,它没有更新 jar 文件。
【解决方案2】:

试试这个

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

【讨论】:

    【解决方案3】:

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

    URL url = getClass().getResource("./beep39.wav");
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    

    然后,在 while 循环中,您将对 AudioInputStream 执行读取方法并将数据发送到 SourceDataLine。

    Java 教程在其audio trail 中对此进行了介绍。此链接跳转到教程的中间。

    AFAIK,Java 7 SDK 中没有“AudioPlayer”。

    【讨论】:

    • 我不得不使用“/audio/filename.wav”而不是“src/audio/filename.wav”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2020-02-11
    • 2017-08-27
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多