用java压缩音乐的Jar?
我尝试将音乐添加到我制作的应用程序中。首先,我尝试使用 .wav
文件,尽管它们变得太大,以至于应用程序变得太大而无法上传到任何地方。
因此,我将文件更改为 .mp3
,尝试了 JMF
和 JLayer
,尽管它们都无法在可运行的 jar 上工作(即使它们可以工作)当我没有导出它们时很好)。
那么有人知道如何使用可运行的 jar 播放压缩音乐吗?
这是 JLayer 的代码,导出后它会在 f = new File(u.toURI())
处停止工作,不会引发任何异常...
try {
URL u = cl.getResource("New Beginnings.mp3");
f = new File(u.toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream(f);
p = new Player(fis);
p.play();
} catch (Exception e) {
System.out.println(e);
}
编辑:通过将上述代码更改为:
try {
InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream(temp+".mp3");
p = new Player(fis);
p.play();
} catch (Exception e) {
System.out.println(e);
}
I've tried to add music to an application I've made. First I tried with .wav
files though they became so huge that the application became too large to upload anywhere.
So I changed the files to .mp3
, tried JMF
and JLayer
though both of them won't work on runnable jars (even if they work fine when I haven't exported them).
So anyone got any tips on how to play compressed music with a runnable jar?
Here's the code for JLayer, when exported it stops working at f = new File(u.toURI())
without throwing any exceptions...
try {
URL u = cl.getResource("New Beginnings.mp3");
f = new File(u.toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream(f);
p = new Player(fis);
p.play();
} catch (Exception e) {
System.out.println(e);
}
Edit: Fixed with changing the above code to:
try {
InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream(temp+".mp3");
p = new Player(fis);
p.play();
} catch (Exception e) {
System.out.println(e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你把声音文件到底放在哪里了?
您应该在项目中创建一个新包并将资源放在那里,然后通过发送完整路径来读取文件:
例如。创建一个名为sounds的新包,然后:
说实话,我没有尝试使用声音,但是当我使用图像时,这个问题发生在我身上。我把它们放在一个包里,一切正常。
Where did you place the sound file exactly?
You should create a new package inside your project and place the resources there, then read the file by sending a complete path:
ex. create a new package called sounds, then:
To be honest, I didn't try it with sounds, but this problem happened to me when I used images. I placed them in a package, and everything worked fine..