Reputation: 450
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
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
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
It's fixed in Java 9, so updating to Java 9 will solve this problem.
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).
Convert your mp3 file into wav format, which is supported by JavaFX MediaPlayer
.
Don't run this on Ubuntu.
Upvotes: 2
Reputation: 159566
Some recommendations:
Platform.runLater
Application
rather than a JFXPanel
. 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