jar 中不播放声音

发布于 2024-10-16 09:37:06 字数 333 浏览 8 评论 0原文

我已将所有类文件和资源打包为 jar,但在执行时声音文件无法播放。我的包结构是:

+project
|_classes
|_ _*.class
|_resources
|_ _ *.jpg,*.wav

代码:

AudioInputStream inputStream = AudioSystem.getAudioInputStream(kidsClassRoom.class.getResourceAsStream("../resources/"+file));

执行此行时获取 null

I have packed all the class files and resources as a jar but on execution the sound files wont play. My package structure is:

+project
|_classes
|_ _*.class
|_resources
|_ _ *.jpg,*.wav

Code:

AudioInputStream inputStream = AudioSystem.getAudioInputStream(kidsClassRoom.class.getResourceAsStream("../resources/"+file));

getting a null when this line is executed!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

薄情伤 2024-10-23 09:37:06

已经提出的理论的替代理论。通常,成功的 getResource() 调用取决于调用来定位它们的类加载器实例。因此,我建议使用用户定义对象的实例,从中调用 getResource()。 EG

// Sanity check
System.out.println("The value of 'file' is: " + file);
// Presuming kidsClassRoom1 is an instance of kidsClassRoom
AudioInputStream inputStream = AudioSystem.
    getAudioInputStream(
        kidsClassRoom1.
            getClass().
            getResourceAsStream("/resources/"+file));

您可能还注意到,代码片段对资源使用前缀“/”。与其他人所说的相反,我相信这意味着“从根”资源路径,无论在运行时类路径上的任何 Jar 中找到它。保留“/”或“../”将使类加载器在发生这种情况的类的子路径中搜索资源。

当然 - 确保 Wav 最终位于 Jar 中!将 .jar 复制/重命名为 .zip 并双击它就是“快速&”。 dirty' 方式来检查 Windows 上的存档内容。

An alternate theory to those already presented. Often successful getResource() calls depend on the class loader instance that is called to locate them. For this reason, I would recommend to use an instance of a user defined object, from which to call getResource(). E.G.

// Sanity check
System.out.println("The value of 'file' is: " + file);
// Presuming kidsClassRoom1 is an instance of kidsClassRoom
AudioInputStream inputStream = AudioSystem.
    getAudioInputStream(
        kidsClassRoom1.
            getClass().
            getResourceAsStream("/resources/"+file));

You might also note that snippet uses the prefix of "/" for the resource. Contrary to what others are saying, I am confident that means 'from the root' of the resource path, in whatever Jar on the run-time class-path it is found. Leaving the '/' or '../' out will have the class loader searching for the resource in a sub-path of the class that this is occurring in.

Of course - make sure the Wav ends up in the Jar! Copy/rename the .jar to a .zip and double click it is the 'quick & dirty' way to examine the archive contents on Windows.

层林尽染 2024-10-23 09:37:06

创建一个名为resources的包,如下所示
在此处输入图像描述

然后

 AudioSystem.getAudioInputStream(kidsClassRoom.class.getResourceAsStream("resources/"+file));

Create a package named resources as shown below
enter image description here

then

 AudioSystem.getAudioInputStream(kidsClassRoom.class.getResourceAsStream("resources/"+file));
毁虫ゝ 2024-10-23 09:37:06

这是我在罐子中播放循环声音文件的功能,它对我来说效果很好。

看来 getResourceAsStream() 不适用于 jar。但是,getResource() 可以。

public synchronized void alarm() {
    try {
        crit = AudioSystem.getClip();
        AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(this.getClass().getResource("critical.wav"));
        crit.open(inputStream1);
        crit.loop(Clip.LOOP_CONTINUOUSLY);

    } catch (Exception e) {
        System.err.println(e.getMessage());
        }
}

This is my function for playing a looping sound file in jars, it works fine for me.

It appears that getResourceAsStream() doesn't work with jars. however, getResource() does.

public synchronized void alarm() {
    try {
        crit = AudioSystem.getClip();
        AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(this.getClass().getResource("critical.wav"));
        crit.open(inputStream1);
        crit.loop(Clip.LOOP_CONTINUOUSLY);

    } catch (Exception e) {
        System.err.println(e.getMessage());
        }
}
没企图 2024-10-23 09:37:06

当您执行 getResourceAsStream 时,它不是相对于当前类,而是相对于存档的根。也就是说,首先尝试删除 ../

When you do getResourceAsStream, it is not relative to the current class, but the root of the archive. That is, first try to remove ../.

污味仙女 2024-10-23 09:37:06

需要注意的重要一点是,在导出的 jar 中,资源不会存储为文件(在某处阅读此内容,请更了解的人输入)。因此,最好首先以 URL 对象的形式获取资源,然后将其传递给 AudioInputStream 对象。

URL url = YourClass.class.getResource(filename);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);

如果资源位于子文件夹中,请记住将其添加到文件名路径中。

The important thing to note is that in the exported jar the resources are not stored as files (Read this somewhere, someone more knowledgeable please input). So it's best to get the resource as a URL Object first then pass that to the AudioInputStream Object.

URL url = YourClass.class.getResource(filename);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);

If the resource is in a subfolder, remember to add it to your filename path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文