0

This is the code i'm trying to execute:

    public void playSound (){
        Media media = new Media(new File(audioFile).toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
        mediaPlayer.play();
    }

(And yes, I know the program doesn't leave the method, but I wanted to see first if the Media object works)

This is the value of the audioFile instance:

this.audioFile = "C:\\(...)\\Pomodoro\\src\\Sounds\\calm.wav";

I'm trying to call this method when a timer reaches zero. But I get the following error message when my Media object is instantiated:

Exception in thread "JavaFX Application Thread" java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module @0x2e23a125) 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 @0x2e23a125

Any suggestion on how to proceed?

I use JavaFX sdk - 20.0.1 and Java jdk-19

I asked ChatGPT for some advice, and it suggested that I should add '--add-exports javafx.graphics/com.sun.glass.utils=ALL-UNNAMED' in the VM-options run configuration. But that didn't help. I got the same error message.

  • 2
    Looks like this may be a version mismatch between the javafx.graphics module and the javafx.media modules. Check you are using the same version for all modules. Post your module-info.java (if you have one) and your pom.xml or gradle.build file. – James_D Jun 22 '23 at 15:27
  • 3
    This isn't the cause of the problem, but the way you are accessing the media file is incorrect. (The `src` folder is not typically available at runtime.) See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – James_D Jun 22 '23 at 15:28
  • 3
    JavaFX classes should not be in the unnamed module. That means you have misconfigured your runtime. The javafx-media jmod (or Maven artifact jar or JavaFX SDK lib directory) should be on your module path and the javafx.media module should be either added as a module in your VM parameters or required in your application's module-info.java. An alternative is to delete your JavaFX SDK and JDK and instead use a JDK that includes JavaFX such as Azul Zulu "JDK FX", then you don't need any special module settings to use JavaFX from your app. – jewelsea Jun 22 '23 at 17:12
  • 2
    "_And yes, I know the program doesn't leave the method_" -- Actually, the program _will_ leave the method, as the media will play asynchronously. Though this fact will lead to another problem once you fix your current error. Since you do not keep a strong reference to the `MediaPlayer`, it will eventually be garbage collected. And when that happens--which _could_ happen immediately--the media will stop playing "unexpectedly". – Slaw Jun 22 '23 at 19:35

0 Answers0