jar 中不播放声音
我已将所有类文件和资源打包为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
已经提出的理论的替代理论。通常,成功的 getResource() 调用取决于调用来定位它们的类加载器实例。因此,我建议使用用户定义对象的实例,从中调用 getResource()。 EG
您可能还注意到,代码片段对资源使用前缀“/”。与其他人所说的相反,我相信这意味着“从根”资源路径,无论在运行时类路径上的任何 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.
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.
创建一个名为
resources
的包,如下所示然后
Create a package named
resources
as shown belowthen
这是我在罐子中播放循环声音文件的功能,它对我来说效果很好。
看来
getResourceAsStream()
不适用于 jar。但是,getResource()
可以。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.当您执行 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../
.需要注意的重要一点是,在导出的 jar 中,资源不会存储为文件(在某处阅读此内容,请更了解的人输入)。因此,最好首先以 URL 对象的形式获取资源,然后将其传递给 AudioInputStream 对象。
如果资源位于子文件夹中,请记住将其添加到文件名路径中。
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.
If the resource is in a subfolder, remember to add it to your filename path.