[英]My .jar file won't open MP3 files (I'm using Jlayer - JZoom library)
I did this small Java project that in it's turn opens different MP3 files. 我做了这个小Java项目,依次打开了不同的MP3文件。 For that I downloaded the JLayer 1.0.1 library and added it to my project.
为此,我下载了JLayer 1.0.1库并将其添加到我的项目中。 I also added the MP3 files to a package on my project -as well as some JPG images- so as to obtain them from there, and I'm using a hashmap (mapa) and this method to get them:
我还将MP3文件以及一些JPG图像添加到了我项目的包中,以便从那里获取它们,并且我正在使用哈希图(mapa)和此方法来获取它们:
public static String consiguePath (int i) {
return AppUtils.class.getClass().getResource("/Movimiento/" + mapa.get(i)).getPath();
}
so as to avoid absolute paths. 以避免绝对路径。
When I open an MP3 file I do this: 当我打开MP3文件时,请执行以下操作:
try {
File archivo = new File(AppUtils.consiguePath(12));
FileInputStream fis = new FileInputStream(archivo);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
Player player = new Player(bis);
player.play();
} catch (JavaLayerException jle) {
}
} catch (IOException e) {
}
The whole thing runs perfectly in NetBeans, but when I build a .jar file and execute it it runs well but it won't open the MP3 files. 整个过程在NetBeans中可以完美运行,但是当我构建一个.jar文件并执行该文件时,它运行良好,但无法打开MP3文件。 What called my attention is that it doesn't have trouble in opening the JPG files that are on the same package.
引起我注意的是,打开同一程序包中的JPG文件不会遇到麻烦。
After generating the .jar I checked the MyProject/build/classes/Movimiento folder and all of the MP3 files were actually there, so I don't know what may be happening. 生成.jar后,我检查了MyProject / build / classes / Movimiento文件夹,实际上所有的MP3文件都在那里,所以我不知道会发生什么。
I've seen others had this problem before but I haven't seen any satisfactory answer yet. 我以前见过其他人有此问题,但尚未见任何令人满意的答案。
Thanks! 谢谢!
Change the consiguePath
to return the resulting URL
from getResource
更改
consiguePath
以从getResource
返回生成的URL
public static URL consiguePath(int i) {
return AppUtils.class.getClass().getResource("/Movimiento/" + mapa.get(i));
}
And then use it's InputStream
to pass to the Player
然后使用它的
InputStream
传递给Player
try {
URL url = AppUtils.consiguePath(12);
Player player = new Player(url.openStream());
player.play();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
Equally, you could just use Class#getResourceAsStream
同样,您可以只使用
Class#getResourceAsStream
Resources are packaged into your Jar file and can no longer be treated as File
s 资源打包到您的Jar文件中,不能再视为
File
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.