Jar with compressed music with java?
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 = 开发者_JAVA技巧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);
}
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:
InputStream fis = ClassLoader.getSystemClassLoader().getResourceAsStream("/sounds/"+temp+".mp3");
p = new Player(fis);
p.play();
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..
精彩评论