0

I'm coding this in java and put my sounds folder with the bgmusic.wav in there but it doesn't seem to still identify it. Am I doing something wrong?

Here is my Sound class:

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;

public class Sound {
    private AudioClip myClip;
    public Sound(String fileName) {
        try {
            File file = new File(fileName);
            if (file.exists()) {
                myClip = (AudioClip) Applet.newAudioClip(file.toURI().toURL());
            } else {
                throw new RuntimeException("Sound: file not found: " + fileName);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException("Sound: malformed URL: " + e);
        }
    }
    public void play() {
        myClip.play();
    }
}

This is then ran as an Object in my other class to run the bgmusic.wav:

String fileName = "bgmusic.wav";
String soundDir = "." + File.separator + "sounds" + File.separator;
String filePath = soundDir + fileName;
Sound bgMusic = new Sound(filePath);
    bgMusic.play();

If I'm not mistaken I think I have everything setup correctly but I get a Sound: file not found: ./sounds/bgmusic.wav which is returned from to throw exception in my Sound class. Since I have the sounds folder as a subdirectory of the main java folder passing ./sounds/bgmusic.wav should be correct right?

user2318083
  • 567
  • 1
  • 8
  • 27
  • 1
    Please post the directory structure of your project, and where the sound file exactly is placed. – Seelenvirtuose Apr 24 '14 at 08:11
  • Does it matter? I was under the impression that when you put `./sounds` that as long as it has a `sounds` folder where the java program is located then it should access it there because of the `.`? – user2318083 Apr 24 '14 at 08:13
  • 1
    See this post that could help you http://stackoverflow.com/questions/23255943/loop-sound-in-java-directory/23256569#23256569 – JHDev Apr 24 '14 at 08:14
  • 1
    @user2318083 The main problem is that you should not load it via `new File`. Such files are resources and should be loaded with `ClassLoader.getResource` or with `Class.getResource`. For a complete answer, we need a little more information. But ... as this was already described a hundred times, you could also easily search for it on SO. – Seelenvirtuose Apr 24 '14 at 08:17

2 Answers2

1

I think you need to have the full path for File to recognize it.

greenhatman
  • 76
  • 1
  • 7
1

The new File(".") represents the base folder (the root folder) of your application. So check your directory structure that where does the file appear with respect to the project base folder. else share your project structure.

If you are placing the file in the java classpath (The src folder) then you can load the sound file from class path (refer here)

Community
  • 1
  • 1
AdityaKeyal
  • 1,208
  • 8
  • 14
  • So when I do `"."` shouldn't it just start where my project folder is at? What exactly do you want when sharing my project structure? My professor showed us this way to do it so I'm just following this but it isn't work correctly for me. – user2318083 Apr 24 '14 at 08:25
  • 1
    can you show the windows-explorer view of the project structure. – AdityaKeyal Apr 24 '14 at 08:29
  • `/Users/*username*/Documents/workspace/Project/src` That's the full directory from my computer then after `src` there's a folder name `b2` where all the java files are. – user2318083 Apr 24 '14 at 08:33
  • 1
    okay. So there are 2 conditions. 1. if file in present under Project/sounds/musicFile.wav - The you can use `new File("./sounds/musicFile.wav")` to access it 2. if file is under Project/src/sounds/musicFile.wav - you need to use class loading `MyClass.class.getResourceAsStream("sounds/musicFile.wav")` – AdityaKeyal Apr 24 '14 at 08:38
  • Gotcha, that is weird because since I couldn't get it working correctly I also put the `sounds` folder under `Project/sounds/` but it didn't work – user2318083 Apr 24 '14 at 08:46
  • 1
    that is wierd.. just check for some spaces. also another way to debug would be to print the file path using : `File file = new File(filePath);System.out.println(file.getAbsolutePath());` this should tell u the exact name of the file being searched by the code. – AdityaKeyal Apr 24 '14 at 08:48
  • Actually it works now, I don't know if it's because I didn't refresh eclipse to allow the folder to show up but I went ahead and refreshed everything in eclipse and it works like it should. Thanks for that debugging method, i'll use that next time. Thanks for your help! :) – user2318083 Apr 24 '14 at 08:50