Search code examples
javamp3media-player

Playing a mp3 in java


I am working on a Java project that involves playing mp3 files. I want my application to play the files from within the project so I have the music files stored in a folder called music which is in a source folder called resources. This is the code I have right now but when I run it I get a Bitstream errorcode 102. I can't seem to figure out what is wrong, any help? I am using the javazoom library (javazoom.jl.player.Player)

public void play() {
    try {
        InputStream stream = MP3.class.getClassLoader()
                .getResourceAsStream("/music/LoveStory.mp3");
        BufferedInputStream bis = new BufferedInputStream(stream);
        player = new Player(bis);
    } catch (Exception e) {
        System.out.println("Problem playing file " + filename);
        System.out.println(e);
    }

    // run in new thread to play in background
    new Thread() {
        public void run() {
            try {
                player.play();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }.start();

}

Solution

  • I actually discovered my problem and it is such a small thing I am quite embarrassed! It turns out when I removed the leading "/" in front of the path name my code ran fine. I believe this had to do with an absolute path vs a relative path.