Christopher Smith
Christopher Smith

Reputation: 450

JavaFX Media Player not working

When I run this code, I get two exceptions. I have Java 8 installed correctly and it is the oracle version (NOT OpenJDK). This same code works fine and plays the media without any problems on Windows with Java 8. I do not want to use an Application because I am trying to play this media from more (Non-JavaFX) code.

import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class Test {
    public static void main(String[] args) {
        new JFXPanel();//Required to initialize JavaFX or I get this exception: Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized
        String fileLocation = "file:/home/chris/Music/jawstheme.mp3";
        System.out.println(fileLocation);
        Media hit = new Media(fileLocation);
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        mediaPlayer.play();
    }
}

And here is the exception I get.

file:/home/chris/Music/jawstheme.mp3
Exception in thread "main" 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 Test.main(Test.java:11)
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:222)
    at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:104)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:467)
    ... 2 more

Upvotes: 1

Views: 6052

Answers (3)

Farrukh Najmi
Farrukh Najmi

Reputation: 5316

I had same issue on JavaFX 13 and Ubuntu 18.04. The solution was easy. Just install the missing mp3 and H.264 codes using the following coommand:

sudo apt-get install ubuntu-restricted-extras ffmpeg

Upvotes: 0

ice1000
ice1000

Reputation: 6579

You didn't say but I bet you're using Ubuntu.

This is a bug of JavaFX.
Bug report: https://bugs.openjdk.java.net/browse/JDK-8150503

Solution 1

It's fixed in Java 9, so updating to Java 9 will solve this problem.

Solution 2

Use org.frice.utils.media.AudioManager.play provided by https://github.com/icela/FriceEngine (it's a 1.6mb jar library, you just have to use it's AudioManager.play, mp3 is supported).

Solution 3

Convert your mp3 file into wav format, which is supported by JavaFX MediaPlayer.

Solution 4

Don't run this on Ubuntu.

Upvotes: 2

jewelsea
jewelsea

Reputation: 159566

Some recommendations:

  • You don't need to (and should not) explicitly add a reference to jfxrt.jar to the classpath if you are using Java 8.
  • Add full error handling to your application as documented in the JavaFX media javadoc.
  • Your code has threading issues - you should interact with JavaFX controls on the JavaFX application thread, see Platform.runLater
  • Use a JavaFX Application rather than a JFXPanel.
  • If you use a JavaFX Application, you will already be on the JavaFX application thread, so a Platform.runLater call would not be necessary in that case.
  • Ensure that you are using the Oracle Java 8 runtime and OpenJDK (check using java -version).
  • Ensure that your system meets the minimum requirements for a certified configuration for JavaFX media playback. In particular:

    "You must install GLIB 2.28 in order to run JavaFX Media. You must install the following in order to support AAC audio, MP3 audio, H.264 video, and HTTP Live Streaming: libavcodec53 and libavformat53 on Ubuntu Linux 12.04 or equivalent."

The exact cause of your issue is not possible to verify given the information you have provided, but hopefully the above recommendations will allow you to resolve your issue.

Upvotes: 1

Related Questions