1

I found many related questions here on stackoverlflow but none seems to work for me. Problem is simple : I try to play a mp3 with javafx mediaplayer. With the code :

@Override
    public void start(Stage theStage) throws MalformedURLException {

// this is the location, copy pasted from fileproperties in explorer :       
//  C:\Users\User\IdeaProjects\gamelogic3\src\main\resources\music.mp3        
        Media media = new Media("file:///Users/User/IdeaProjects/gameLogic3/src/main/resources/music.mp3");
        //Media media = new Media(MEDIA_URL);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();

with results in : Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module @0x160d9abc) cannot access class com.sun.glass.utils.NativeLibLoader (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.utils to unnamed module @0x160d9abc at com.sun.media.jfxmediaimpl.NativeMediaManager.lambda$new$0(NativeMediaManager.java:110) at java.base/java.security.AccessController.doPrivileged(AccessController.java:554) ...

At first, I tried it the most convient way : (to load it from the resources) :

@Override
  public void start(Stage primaryStage) {
    final URL resource = getClass().getResource("music.mp3");
    final Media media = new Media(resource.toString());
    final MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.play();

But that result in a nullpointer exception : Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "resource" is null at mainStartup.Main.start(Main.java:57) What is kind of weird because this came directly out of a tutorial : http://www.java2s.com/Code/Java/JavaFX/Playmp3file.htm

I tried it with 2 more files (mp3 and wav) but same results. I even copied the files to the root folder and changend the url's but I can open the mp3 from the resource files in Intellij. When I rename the mp3 file inside Intellij the name in my code change along with it, so I believe the path is correct.

The project is using openjdk-15 language level 15-Text blocks on windows 10 machine , without the code for the sound the javafx project is working for the animation.

enter image description here

Any help appreciated

1 Answers1

1

The resource being null is probably because you give a relative path to Class.getResource(), meaning it will look in the directory corresponding to the class' package. Try using getResource("/music.mp3") instead (note the slash).

You will then, however, run into your original problem again, which is caused by the javafx.media module not being accessible. I believe this answer should help with that.

OhleC
  • 2,821
  • 16
  • 29