0

I am making an little Java music player application in netbeans(not for android). we have made an asset folder within the project with the sings in it the music player should play. we have all functions working, exept we can't figure out how to call up the song from within the project map. we now have hardcoded it in on a specific location, but that wont work on a different computer.

Does someone have a solution so that we can call up the song from within our project map?

  • I'm not quite sure I understood what you mean, but what about a properties file which could be defined by the user? You could set up the path to the songs in that properties file and load it from the application. – Keews Oct 28 '15 at 11:58
  • I think you're looking for [this](http://stackoverflow.com/questions/14209085/how-to-define-a-relative-path-in-java). If I understand correctly, you just want a relative path to the music. – Avantol13 Oct 28 '15 at 12:02

1 Answers1

0

Given that you have an Assets folder in your project, you could do something like this:

initializeAudio("Assets/my_cool_song.wav");

where initializeAudio relies on a class-level declaration of a Clip object:

private void initializeAudio(final File url) {
    try {
        if (clip != null) stopPlay();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(url);
        clip = AudioSystem.getClip();
        clip.open(inputStream);
    } catch (Exception e) {
        stopPlay();
        System.err.println(e.getMessage());
    }
}

The reason that this works on all computers with the project (or JAR), and initializeAudio("/Users/myusername/Developer/Projects/AudioApp/Assets/my_cool_song.wave"); doesn't work is because one relies on a hard-coded absolute path, and the other relies on a file-path relative to the structure of the project. When creating relative paths, pretend that the base directory of your project is all that exists.

So, for example, if your project structure is a MyProject folder with children Assets, src, bin, and util, and you wanted to refer to some item within the util folder, pass along the path like so: util/my_cool_file.jpeg

wadda_wadda
  • 966
  • 1
  • 8
  • 29