0

I'm trying to access the MP3 files that are located inside the resources folder insidea jar file. I got some issues, they can't be readed.

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;

public class MP3 {

    private static final Logger logger = Logger.getLogger(Main.class.getName());


    private BasicPlayer player;


    /**
     * Constructor
     */
    public MP3() {
        player = new BasicPlayer();
    }

    public void changeSong(song) {

        // Change song now
        String pathToMp3 = System.getProperty("user.dir") + this.getClass().getResource(song);
        try {
            player.open(new URL("file:///" + pathToMp3));
            player.play();
            logger.info("Playing the song: " + song);
        } catch (MalformedURLException | BasicPlayerException e) {
            logger.info("Could not turn on MP3 music from: " + pathToMp3);
        }

    }

}

The pathToMp3 looks like this: enter image description here

I have solved a similar issue with CSV read. Instead of reading from resources. I read the resources as streams.

InputStream in = getClass().getResourceAsStream("/file.txt"); 
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

But the problem here is that

layer.open(new URL("file:///" + pathToMp3));

Takes the argument URL, not stream or string. Do you know how to stream MP3:s from the resources folder inside a executable JAR file?

Update:

       // Change song now
        String pathToMp3 = getClass().getResource(song).toString();
        InputStream in = getClass().getResourceAsStream(song);
        try {
            player.open(in);
            player.play();
            logger.info("Playing the song: " + song);
        } catch (BasicPlayerException e) {
            logger.info("Could not turn on MP3 music from: " + pathToMp3);
        }

Gives the error:

enter image description here

euraad
  • 2,467
  • 5
  • 30
  • 51
  • 1
    Since `this.getClass().getResource(song);` returns a `URL`, have you considered just trying `player.open(this.getClass().getResource(song))`? – MadProgrammer Dec 19 '19 at 19:34
  • Also, check the `song` value, for simplicity, make sure it's a absolute path from the top of you package structure to your resource, ie `/resources/HappyHappJoyJoy.mp3` – MadProgrammer Dec 19 '19 at 19:36
  • @MadProgrammer It gave the same error as before. – euraad Dec 19 '19 at 19:38
  • Perhaps use the ```Player``` class instead. Refer: https://stackoverflow.com/a/24337995/11226302 for the code – Shivam Puri Dec 19 '19 at 19:38
  • @ShivamPuri I found out that the current object have a file stream argument as well. But that didn't solve my problem. – euraad Dec 19 '19 at 19:45
  • I updated the question. Look for the **update** – euraad Dec 19 '19 at 19:45
  • @DanielMårtensson Look at the path. Is `/se/danielmartensson/pijuckbox/Song0.mp3` the correct location for the file (within your Jar)? I'd unzip the Jar a verify the location of the file (and ensure it's been included) – MadProgrammer Dec 19 '19 at 19:55
  • 1
    Also perhaps you should try printing the stack trace in the catch block. Maybe that gives a better idea of what is happening! ```e.printStackTrace();``` – Shivam Puri Dec 19 '19 at 19:56
  • @MadProgrammer That's correct path. It's a se.danielmartensson.pijukebox package in Java. But I tried to read the exception now and I got "javax.sound.sampled.LineUnavailableException" – euraad Dec 19 '19 at 19:57
  • @ShivamPuri Yes. The stacktrace is "javax.sound.sampled.LineUnavailableException" – euraad Dec 19 '19 at 19:58
  • 1
    Notice that this is on a Raspberry Pi. But Java runs everywhere ;) – euraad Dec 19 '19 at 19:58

1 Answers1

1

I copied the BasicPlayerTest example from here and modified the play method to take an URL.

Then running the main works from the project and after the export in the jar.

Note that I copied the resources folder inside the src folder.

public static void main(String[] args) {
    BasicPlayerTest test = new BasicPlayerTest();
    test.play(test.getClass().getResource("/resources/test.mp3")); 
}

The url I am seeing in the log is:
jar:file:/C:/Temp/mp3.jar!/resources/test.mp3

second
  • 4,069
  • 2
  • 9
  • 24
  • 1
    Hmm. Thanks! I tried that on my Ubuntu computer. It works! But not on Raspberry Pi. – euraad Dec 19 '19 at 20:13
  • You might get a better answer on the [raspberrypi stackexchange](https://raspberrypi.stackexchange.com/). Does it actually work if you put the mp3 outside of the jar? – second Dec 19 '19 at 20:39