0

I was trying to use MP3s instead of wavs and it works fine in NetBeans, but when I build it and try to run the jar there's no sound and I get the NoPlayerException.

    background = ImageIO.read(getClass().getResourceAsStream("/background1.png"));
    sun = ImageIO.read(getClass().getResourceAsStream("/sun.png"));
    cloud = ImageIO.read(getClass().getResourceAsStream("/cloud.png"));
    pause = ImageIO.read(getClass().getResourceAsStream("/pause.png"));
    soldierchant = AudioSystem.getAudioInputStream(getClass().getResource("/SoldiersChant.wav"));
    thebreach = AudioSystem.getAudioInputStream(getClass().getResource("/TheBreach.wav"));
    forever = AudioSystem.getAudioInputStream(getClass().getResource("/Forever.wav"));

    Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
    Format input2 = new AudioFormat(AudioFormat.MPEG);
    Format output = new AudioFormat(AudioFormat.LINEAR);
    PlugInManager.addPlugIn("com.sun.media.codec.audio.mp3.JavaDecoder", new Format[]{input1, input2}, new Format[]{output}, PlugInManager.CODEC);
    try {
        Player player = Manager.createPlayer(new MediaLocator(getClass().getResource("/TheBreach.mp3").toURI().toURL()));
        player.start();
    } catch (IOException ex) {
        ex.printStackTrace();
    }catch (java.net.URISyntaxException x) {
        x.printStackTrace();
    }catch (javax.media.NoPlayerException c) {
        c.printStackTrace();
    }

As you can see I'm getting the file from using getResource just like the images and wav that work both in NetBeans and jar. Could this be the problem com.sun.media.codec.audio.mp3.JavaDecoder

I'm grabbing at straws at this point. I've tried putting the MP3 in every folder and doing no / and the full directory.

This is my Exception's getMessage:

Cannot find a Player for jar:file:/C:/Users/Patrick/Documents/NetBeansProjects/PatBuild8FX/dist/PatBuild8FX.jar!/TheBreach.mp3

I think it is Manager.createPlayer because when I just create the file alone its fine, but when I try to create a player with it, it doesn't work.

 File m = new File("file:/C:/Users/Patrick/Documents/NetBeansProjects/PatBuild8FX/dist/PatBuild8FX.jar/TheBreach.mp3");
    System.out.println(m);
    System.out.println(Manager.createPlayer(m.toURI().toURL()));

file:\C:\Users\Patrick\Documents\NetBeansProjects\PatBuild8FX\dist\PatBuild8FX.jar\TheBreach.mp3
java.io.IOException: File Not Found
java.io.IOException: File Not Found
Exception in thread "main" javax.media.NoPlayerException: Error instantiating class: com.sun.media.protocol.file.DataSource : java.io.IOException: File Not Found
at javax.media.Manager.createPlayerForContent(Manager.java:1362)
at javax.media.Manager.createPlayer(Manager.java:417)
at javax.media.Manager.createPlayer(Manager.java:332)
at Build8.PanGame.<init>(PanGame.java:75)
at Build8.Main.main(Main.java:30)
double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

0

Did you package the 'TheBreach.mp3' file into your jar file? The exception message above suggests that the resource is being accessed from your jar file.

My suggestions:

  • if you haven't already done so, package the .mp3 file into the jar file (this makes it easier for you to move the single jar file without having to copy the .mp3 file along with it, and also the mp3 file should now be accessible from the jar file), or

  • if you prefer to have the .mp3 file where it is, then try using a java.io.File class to load the mp3 file before passing to the MediaLocator constructor. Hence, you'd then have something like: new MediaLocator(new File("file:///C:\\full_path_to_the_file\\TheBreach.mp3").toURL())

  • finally, the documentation for MediaLocator.createPlayer() explaining issues with creating players should be handy. You can find it here

Good luck!

Ibrahim Dauda
  • 645
  • 8
  • 19
  • I am already packaging the mp3 with the jar, I used WinRAR to open the jar and its in there. The full directory doesn't work. –  Jan 19 '14 at 15:55