Results 1 to 6 of 6
Thread: Importing Sounds
- 05-16-2013, 05:06 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Importing Sounds
I'm somewhat new to java. I have been programming for about a year now. But, I want to add a sound file to my java project and it says FileNotFoundException. Is this do to importing the sound wrong or is eclipse just being retarted again. I have the sound in a package called game.res.sounds and I access the sound using the AudioPlayer Class. Any suggestion would help a lot. Please and Thank you.
- 05-16-2013, 05:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,717
- Rep Power
- 16
Re: Importing Sounds
A FileNotFoundException pretty much means what it says: the named file does not exist.
There are two very common causes for this. The first is that the file you named was not the file you meant to name. Specifically you are naming a file in one directory, but the file you want exists in another directory. You can check exactly what file you have named with the File instance by printing it to System.out.
Java Code:// do whatever you do to get a File instance File file = ... // ... later System.out.println("file is " + file.getAbsolutePath()); // now do whatever you want with the file
---
To start with I'd suggest you use getAbsolutePath() and veerify for yourself whether he file you are naming actually exists or not.
- 05-17-2013, 04:09 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: Importing Sounds
I'm sorry I said my question wrong lol. I meant my sound runs fine on eclipse but, once I export it as a jar file it does not play. I put the sound in a source folder called res then into a sub folder called soundres.
- 05-17-2013, 09:44 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
Re: Importing Sounds
OK.
And is the sound file in the jar file?
If so then the bit above about not using File stands...you should be using resources (getResource and getResourceAsStream).Please do not ask for code as refusal often offends.
** This space for rent **
- 05-19-2013, 02:25 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: Importing Sounds
How do I find out if its in the .jar file?
- 05-19-2013, 10:25 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,717
- Rep Power
- 16
Re: Importing Sounds
A jar file is a sort of zip archive. If your operating system/desktop doesn't easily let you see the contents, copy the jar file and rename it so that it has a .zip extension.
Also there is quite a lot of discussion of jar archives in Oracle's Tutorial in the Packaging Programs in Jar Files. This gives both commands to list a jar file's contents, and instructions for creating the jar file for a program that includes things like sound resources. Compiling the program and creating the jar file all at the command line will teach you a lot about what an IDE is doing "behind the scenes".
Similar Threads
-
Making Sounds
By Humphrey Bogart in forum New To JavaReplies: 1Last Post: 09-05-2012, 12:20 AM -
Playing sounds on other computers
By jeffpaulwilson in forum AWT / SwingReplies: 0Last Post: 09-14-2011, 09:25 PM -
Sounds?
By Alerhau in forum New To JavaReplies: 3Last Post: 08-29-2011, 07:30 PM -
How can I loop sounds in JPanel?
By rajkobie in forum New To JavaReplies: 4Last Post: 04-24-2011, 03:49 PM -
Wave Sounds
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-22-2008, 01:44 PM
Bookmarks