1

I'm new to java and I'm trying to make my javafx application play a sound. I'm not being able though to create a javafx.scene.media.Media object as I keep getting the exception.

java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\Cliente\.jdks\corretto-1.8.0_252\jre\bin\glib-lite.dll

Here is the piece of code that is generating this error.

Media sound = new Media(new File("./data/audio/Bomb.mp3").toURI().toString());

I imagine that this is being generated by my uri being wrongly formatted but I can't see why this is different from tutorials I've seen. My audio file finds itself in /data/audio which is inside the root folder of the project. Can anyone help me?

Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
Victor Kang
  • 19
  • 1
  • 2
  • 1
    Which JVM and JDK are you using? Does the file `C:\Users\Cliente\.jdks\corretto-1.8.0_252\jre\bin\glib-lite.dll` exist on your harddrive? What is your operating system (Windows, but which one)? Does the exception came with a stack trace? If so, please add it here! Have you access to other JDKs? Can you try your code with these? – tquadrat May 02 '20 at 10:14
  • As it relates to the music file, you need to determine if it is packaged with the jar or not and follow the answer for your case. https://stackoverflow.com/questions/24347658/getting-a-mp3-file-to-play-using-javafx. If you know that it is packaged with the jar, the following is a great resource. https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – SedJ601 Jan 28 '21 at 16:07

2 Answers2

4

I think your problem has nothing to do with the creation of the Media object.

Amazon Corretto for Java 8 does not support JavaFX. Please, see the following Github issue and this comment:

Hi. The build of OpenJFX 8 in Corretto 8 is currently on life support and we only plan to apply security patches and critical fixes. Upstream, OpenJFX 8 is abandoned (see https://hg.openjdk.java.net/openjfx).

The recommended way of using JavaFX is with Corretto 11 and pulling in OpenJFX separately e.g. with a Maven dependency. The latest version (currently 14) is compatible with Corretto 11.

Closing this issue.

Please, follow the suggested approach, or use a different Java distribution instead.

Please, see also this 1 2 SO questions, I hope they will be help.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • thanks yes it looks like it is the issue since i'm using JDK 8 which is old – Asmoun Jan 27 '21 at 13:58
  • 1
    You are welcome @Asmoun, thank you. I am happy to hear that the answer was helpful. – jccampanero Jan 27 '21 at 14:08
  • 1
    @Asmoun Just for clarification, I do not thing the problem is whether the JDK is old or not, it has to do with the Java 8 version of Amazon Corretto, but for Java 8 you can find other JVM distribution that should work properly as suggested in the above-mentioned issue as well. You can of course switch to a recent version of Amazon Corretto as also indicated in the answer. – jccampanero Jan 27 '21 at 19:39
2
Media sound = new Media(new File("./data/audio/Bomb.mp3").toURI().toString());
                                                                     ^ issue.

use

Media sound = new Media(new File("./data/audio/Bomb.mp3").toURI().getPath());
EverNight
  • 964
  • 7
  • 16
  • 1
    No. The `javafx.scene.media.Media` constructor takes a URI not a file path. If there is no scheme then it assumes the path is a _resource path_ (i.e. relative to the class-path). – Slaw Jan 27 '21 at 16:44
  • 2
    well ... there go my dreams of acquiring 50 internet points .... – EverNight Jan 27 '21 at 17:43
  • @EverNight no worries 50 points will split between you two , so you will have 25 – Asmoun Jan 27 '21 at 19:50
  • 1
    Thanks @Asmoun , but my answer was besides the point. I don't think I should be getting any points for it. :) should'a probably spent more than 5 minutes checking the context out. It's only fair the other guy gets 100% – EverNight Jan 28 '21 at 06:51