Ubuntu: Could not create JavaFX media issues on the player

Ubuntu: Could not create JavaFX media issues on the player … here is a solution to the problem.

Ubuntu: Could not create JavaFX media issues on the player

I’m trying to play mp3 files on JavaFX-11.0.2 on Ubuntu-20.04 on Ubuntu-11.0.2 using JDK-13.0.2 and I’m getting the same error every time. While searching, I found that JavaFX used older versions of some libraries (See this issue on github )。 So I compiled and installed ffmpeg-3.1.11 from source. But throws the same exception. I put my code and error message below. I stuck with it for days, so I really appreciate any help. Thank you.

Note: This is not a duplicate issue. All other issues regarding this error occur on different versions and have different causes. I tried most of them, but none of them worked for me.

/usr/lib/jvm/jdk-13.0.2/bin/java --module-path /opt/javafx-sdk-11.0.2/lib --add-modules=javafx.controls,javafx.fxml,javafx.media -Djava.library.path=/opt/ javafx-sdk-11.0.2/lib -javaagent:/opt/idea-IU-201.7846.76/lib/idea_rt.jar=46237:/opt/idea-IU-201.7846.76/bin -Dfile.encoding=UTF-8 -classpath /home/parsa/Documents/ welanner/target/classes:/opt/javafx-sdk-11.0.2/lib/src.zip:/opt/javafx-sdk-11.0.2/lib/javafx-swt.jar:/opt/javafx-sdk-11.0.2/lib/javafx.web.jar:/opt/javafx-sdk-11.0.2/ lib/javafx.base.jar:/opt/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/opt/javafx-sdk-11.0.2/lib/javafx.media.jar:/opt/javafx-sdk-11.0.2/lib/javafx.swing.jar:/opt/ javafx-sdk-11.0.2/lib/javafx.controls.jar:/opt/javafx-sdk-11.0.2/lib/javafx.graphics.jar:/home/parsa/.m2/repository/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar view . GUI
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.media/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
    at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:518)
    at javafx.media/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:421)
    at view. GUI.start(GUI.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:295)
    at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
    at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:474)
    ... 11 more
Exception running application view. GUI

Process finished with exit code 1

Here is my code. I know the path and other stuff is correct because I can run it properly on Windows.

package view;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import java.io.File;

public class GUI extends javafx.application.Application{
    public void start(Stage stage) throws Exception {
        String path = "/home/parsa/Music/Ninoush.mp3";
        Media media = new Media(new File(path).toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        stage.setTitle("Playing audio");
        stage.show();
    }

public static void main(String[] args) {
        launch(args);
    }
}

Here is my ffmpeg and library version:

ffmpeg version 3.1.11 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-10ubuntu2)
configuration: 
libavutil      55. 28.100 / 55. 28.100
libavcodec     57. 48.101 / 57. 48.101
libavformat    57. 41.100 / 57. 41.100
libavdevice    57.  0.101 / 57.  0.101
libavfilter     6. 47.100 /  6. 47.100
libswscale      4.  1.100 /  4.  1.100
libswresample   2.  1.100 /  2.  1.100

Edit 3: I can play WAV files this way, but I can’t play MP3.

Solution

Another solution is to update JavaFX, which 16 can successfully play MP3 files on Ubuntu 20.
You can download it from here: javafx download link

Related Problems and Solutions