0

How come my file isn't being read correctly? I checked, and the res folder is a resource in this project.

public class Testing {

    private File file;
    private Clip clip;
    private AudioInputStream audioIn;

    public Testing() {
        String path = "/res/shot.mp3";
        file = new File(path);
        System.out.println(file.toString());
        try {
            audioIn = AudioSystem.getAudioInputStream(file);
            clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
        } catch(Exception e) {
            e.printStackTrace();
        }
        // java.io.FileNotFoundException: \res\shot.mp3 
        // (The system cannot find the path specified)
    }

    public static void main(String[] args) {

        new Testing();
    }
}

My Package Explorer.

enter image description here

I tried changing the path to /SoundTest/res/shot.mp3, still no luck. Any ideas?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Possible duplicate of [Java new File() says FileNotFoundException but file exists](https://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists) – Shanu Gupta Jul 03 '18 at 04:59
  • No luck. I get the same error, except the path is now obviously shot.mp3 in the console. – Michael Kellam Jul 03 '18 at 05:00
  • 2
    `/res/shot.mp3` is an absolute path. This will only work if `res` is in the root directory of your file system. You say "The resource folder is a folder in this project". Where is the project located? – geco17 Jul 03 '18 at 05:02
  • Is this file must be a real file or will package in the jar ? – Arnault Le Prévost-Corvellec Jul 03 '18 at 05:03
  • @WilliamBurnham The project is in one of my eclipse workspace folders, then in that folder, is the bin, src, etc., as well as my res folder. – Michael Kellam Jul 03 '18 at 05:11
  • @MichaelKellam change folder name from `res` to `resources` and use path as `shot.mp3` only. – Shanu Gupta Jul 03 '18 at 05:21
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jul 03 '18 at 05:43

3 Answers3

1

/res/shot.mp3 is an absolute path. This will only work if res is in the root directory of your file system. You say "The resource folder is a folder in this project". From your screenshot, the directory structure is something like

/home/your_user_dir/your_project_dir
                                    /src ...
                                    /bin
                                    /res

So you need to change the creation of your File object such that either it has the correct relative path, or uses an absolute path.

You can use

InputStream is = this.getClass().getClassLoader().getResourceAsStream("/res/shot.mp3");

and then create your AudioInputStream with AudioSystem.getAudioInputStream(InputStream) -- documentation

So change your code to

    String path = "/res/shot.mp3";
    InputStream is = this.getClass().getClassLoader().getResourceAsStream(path);
    try {
        audioIn = AudioSystem.getAudioInputStream(is);
        clip = AudioSystem.getClip();
        clip.open(audioIn);
        clip.start();
    } catch(Exception e) {
        e.printStackTrace();
    }
geco17
  • 5,152
  • 3
  • 21
  • 38
  • 1
    *"This will give you a url that you can convert to a file object,"* Not a) if it's coming from the web or b) **inside a Jar.**. An URL can cover many more sources of data than a file can, so once the code gains a valid URL, either get an input stream directly from it or (safer with Java Sound, which often requires a buffered input stream) use the URL directly. – Andrew Thompson Jul 03 '18 at 05:47
  • @AndrewThompson thanks for your comment, I updated the answer – geco17 Jul 03 '18 at 05:49
0

Regardless of the file beeing present, are you sure you want to use a .mp3-file? AudioSystem.getAudioFileTypes() returns WAVE, AU and AIFF as supported types on my machine.

Furthermore is the exception you are presenting in your question matching the source code you shared? In your code its shot.mp3, but the exception refers to a .wav file.

sn42
  • 2,353
  • 1
  • 15
  • 27
0

No you cannot use

file = new File(path);
audioIn = AudioSystem.getAudioInputStream(file);

If you want to get the file in the resources folder you have to get using the Method getResource()

Change this in your code :

    URL url = Testing.class.getResource("/res/shot.mp3");
    AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
    Clip clip = AudioSystem.getClip();
    clip.open(audioIn);//surround with try catch block i didn't use here

hope this may helpful

Dhanasekaran Don
  • 294
  • 1
  • 14