Java executable .jar file does not play MP3 file from external resource

663 views Asked by At

I have a Java project in Netbeans which includes a User interface. From the interface the user can play an MP3 file that is stored in a folder "data" within the projects working directory. For playing the file I have a class that creates an MP3 player and makes use of the JLayer.jar.

import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javazoom.jl.player.Player;


public class MP3Player {


private String filename;
private Player player; 

// a default constructor
public MP3Player() {}

// Constructor takes a given file name 
public MP3Player(String filename) {
    this.filename = filename;
}

public void close() { if (player != null) player.close(); }

// play the JLayerMP3 file to the sound card
public void play() {
    try {
        InputStream fis     = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        player = new Player(bis);
    }
    catch (Exception e) {
        System.out.println("\n Problem in playing: " + filename);
        System.out.println(e);
    }
}

public void play(String mp3filename) {
    try {
        InputStream fis     = new FileInputStream(mp3filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        player = new Player(bis);
    }
    catch (Exception e) {
        System.out.println("Problem in playing: " + mp3filename);
        System.out.println(e);
    }

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

Within the UI class I have a play button with an action method within which I create an MP3 player object. I then pass the filepath to the MP3 Player

MP3Player mp3 = new MP3Player();  
mp3.play("data/audio/" + filepath);

As long as I run this project in Netbeans, it works fine and the music plays. But once I create a jar file it does not play the music. I found some other posts about similar problems For example: Loading an mp3 file with JLayer from inside the Jar

But contrary to them I don't want to include the MP3 files in the JAR. It should take the files from a local folder "data".

I place the "data" folder containing the mp3.files in the same directory as the jar file. So the filepath is exactly the same as when I have it in the Netbeans project's working directory and run it from Netbeans (as I said there is no problem then). And the JAR gets all the other data like textfiles and images without any problems from the local "data" folder.

Any help is highly appreciated.

1

There are 1 answers

0
Pranjal Choladhara On

Netbeans creates dirctory like this: YourProjectDir/dist/YourProject.jar. Your libraries are placed inside YourProjectDir/dist/lib/otherlibrary.jar. Create your data folder inside your YourProjectDir i.e. your media files will remain inside YourProjectDir/data/audio/.

What you do, just create a batch file with any name.

@echo off 
START/MAX dist\YourProject.jar

Place the Batch file inside YourProjectDir. Now run the Batch file.