No sound in executable jar
Ok now this is a real problem I believe. When I run this in eclipse it works fine, but once I export it to a jar the sound refuses to work. I have the 3 .wav files in a resources folder and I have a button that toggles the String 'file' between them.
Relevant code:
Code :
private class AudioClip extends Thread implements ActionListener{
//private InputStream in;
private AudioStream as;
public void run(){
//Open audio stream
try{
//in = this.getClass().getResourceAsStream("/resources/" + file);
//as = new AudioStream(in);
as = new AudioStream(this.getClass().getResourceAsStream("/resources/" + file));
AudioPlayer.player.start(as);
Timer time = new Timer(5000, this);
time.setRepeats(false);
time.start();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event) {
AudioPlayer.player.stop(as);
}
}
Re: No sound in executable jar
Are the sound files in the jar file and are they in the correct folder?
Re: No sound in executable jar
I think so? And yeah I only have 'resources'.
Re: No sound in executable jar
Use a zip utility to look inside the jar file and make sure the files are where you expect them to be.
BTW AudioClip is a poor name for your class. Java Se has a class with the same name.
Re: No sound in executable jar
Yeah I figured, I was just running out of sound-related names haha. And yeah Izarc shows everything's there.
Re: No sound in executable jar
Turns out the difference between 'r' and 'R' kept me from completing this project! Now i'm just glad i'm finally done.
Re: No sound in executable jar
Quote:
Originally Posted by
BestSanchez
Turns out the difference between 'r' and 'R' kept me from completing this project! Now i'm just glad i'm finally done.
Always keep in mind that the language is case-sensitive :)
Re: No sound in executable jar
Quote:
Originally Posted by
BestSanchez
Turns out the difference between 'r' and 'R' kept me from completing this project! Now i'm just glad i'm finally done.
Windows can be a bit misleading in this regard.
getResource() returns you a URL and if this is a file: URL and you are on Windows case doesn't matter. Your program does the right right thing until... you create a jar archive. Then you are dealing with a jar: URL and case does matter for the names of entries in a such an archive.
It pays to have a convention (all lower case with underscores, or camelCase like Java) for resource files.
Re: No sound in executable jar
It was actually the name of the folder 'Resources' #-o
Re: No sound in executable jar
Ah! Same thing applies, of course: directories also "map" to archive elements with case being honoured in the archive, but not in the (Windows) file system.
In this case I think the "normal" thing to do would be to change the folder (directory) name, not the code. This would be in keeping with Java package naming conventions, and also how IDEs do things ("bin" and "src", not "Bin" and "Src").