1

I've set up a media player that plays a certain media file from a path in the program's directory. This runs in the Init method of a Netbeans JavaFX application (Basically it calls init after loading the FXML graph)

public static final Path APPLICATION_AUDIOTRACK = Paths.get("Audio", "L.I.F.T.mp3");
Media media = new Media(APPLICATION_AUDIOTRACK.toUri().toString());
this.mediaPlayer = new MediaPlayer(media);
this.mediaPlayer.play();

This works just fine (Audio plays, everything good) in the Netbeans environment, but when I try to run the standalone jar (After copying the jar from the dist folder after clean & build to the main program directory) I get this when running it:

Exception in thread "JavaFX Application Thread" MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:511)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:414)
    at com.protonmail.sarahszabo.formattool.core.Main_PanelController.lambda$initialize$2(Main_PanelController.java:190)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:274)
    at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:467)
    ... 9 more

Some notes about this:

  1. Nowhere here does there appear to be anything wrong with the code per se, I get the feeling that i'm missing something subtle.

  2. The jar was moved to the main program directory (As opposed to the dist folder that Netbeans makes automagically). I don't think that this is a library issue either (I do have some dependencies on Apache IO Commons), but the lib folder was moved as well, and this doesn't appear to be related to any lib issue as far as I can tell.

  3. I'm running this on Kubuntu 17.10. I've heard that previously x64 *buntu systems were unsupported on JRE 8, but I think that that post is dated (Over a year old: JavaFX media player on Ubuntu 16.04).

EDIT 0: I have glib-2.0 & libavcodec57 installed (Upon recommendation of: https://stackoverflow.com/a/35872444/2058221).

Sarah Szabo
  • 10,345
  • 9
  • 37
  • 60
  • `Paths.get("Audio", "L.I.F.T.mp3")` is a relative path, which means it depends on the *current directory* of the Java process. That may or may not be the directory where the .jar file resides. For this reason, it’s best to embed the file in your .jar; such a .jar entry is called an application resource, and you access it not as a Path, but as a URL returned by [Class.getResource](https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getResource-java.lang.String-). – VGR Apr 08 '18 at 18:31
  • @VGR Hmm, where is the default Jar perspective then? I assumed that it set itself to wherever the jar file is. The only reason why I moved it in the first place is because I figured that Netbeans set the default perspective to the project folder. I also tried calling `toAbsolutePath`, before the `.toURI`, but no luck. – Sarah Szabo Apr 08 '18 at 18:39
  • You have no guarantee of what the current directory is. That’s why you should not rely on it. If you place the mp3 file inside your .jar file as a resource, you no longer have to worry about where your .jar is located. – VGR Apr 08 '18 at 18:42
  • @VGR Is there any way to do that with Netbeans? I've never done such a thing. – Sarah Szabo Apr 08 '18 at 18:56

0 Answers0