Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

java - JavaFX: "Toolkit" not initialized when trying to play an mp3 file through MediaPlayer class

I'm trying to make a simple mp3 play in the background of my program using the following:

Media med = new Media(getClass().getResource("intro.mp3").toExternalForm());
MediaPlayer mPlayer = new MediaPlayer(med);
mPlayer.play();

The intro.mp3 file is placed in the bin folder of my package, along with the other .class files.

The problem is that my program terminates with:

Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized

Full termination log is:

Device "Intel(R) HD Graphics Family" (\.DISPLAY1) initialization failed : 
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302

Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:153)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:148)
    at javafx.application.Platform.runLater(Platform.java:52)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:450)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:365)
    at PokerApp.<init>(PokerApp.java:33)
    at PokerApp.main(PokerApp.java:105)

Anybody have any ideas as per the cause of the problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JavaFX performs "hidden" initialization on start. Running MediaPlayer doesn't trigger initialization.

The easiest ways to trigger it are:

  • have Application.launch() executed
  • have Application based program being run from jar packaged by fx ant tasks (e.g. built from Netbeans JavaFX project)
  • have JFXPanel started
  • call Platform.startup(Runnable) (Java 9+)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...