Results 1 to 6 of 6
  1. #1
    dylan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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
    The second reason is a bit more subtle. If your program is running as an executable jar (which the sound resource included as part of the jar archive) then you should not be using a File instance at all. That's because the entries in a jar archive are not files - they are resources and you access them with a URL instance. A lot of methods that deal with sounds and images come in two flavours - one using a File or String (filename), and another using a URL.

    ---

    To start with I'd suggest you use getAbsolutePath() and veerify for yourself whether he file you are naming actually exists or not.

  3. #3
    dylan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default 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 **

  5. #5
    dylan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Importing Sounds

    How do I find out if its in the .jar file?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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

  1. Making Sounds
    By Humphrey Bogart in forum New To Java
    Replies: 1
    Last Post: 09-05-2012, 12:20 AM
  2. Playing sounds on other computers
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2011, 09:25 PM
  3. Sounds?
    By Alerhau in forum New To Java
    Replies: 3
    Last Post: 08-29-2011, 07:30 PM
  4. How can I loop sounds in JPanel?
    By rajkobie in forum New To Java
    Replies: 4
    Last Post: 04-24-2011, 03:49 PM
  5. Wave Sounds
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-22-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •