JavaFX Media Player not working


Question

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
1
1
11/14/2014 12:34:28 PM

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.

2
12/17/2017 1:46:06 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon