JavaFX:尝试通过MediaPlayer类播放mp3文件时未初始化“Toolkit”

时间:2012-12-24 21:10:49

标签: java media-player javafx-2

我正在尝试使用以下内容在我的程序后台进行简单的mp3播放:

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

intro.mp3文件与其他.class文件一起放在我的包的bin文件夹中。

问题是我的程序终止于:

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

完整终止日志是:

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)

根据问题的原因,有人有任何想法吗?

4 个答案:

答案 0 :(得分:25)

JavaFX在启动时执行“隐藏”初始化。运行MediaPlayer不会触发初始化。

触发它的最简单方法是:

答案 1 :(得分:14)

要避免初始化异常,您必须调用 Application.launch()方法或只需实例化新的JFXPanel()类 (即使它不是'用于任何事情)。这将在应用程序启动时启动 JavaFxRuntime

要实例化JFXPanel,请在代码中添加以下行

import javafx.embed.swing.JFXPanel;

导入以下包

            let urlPath: String = "puffin://www.facebook.com"

            let url: NSURL = NSURL(string: urlPath)!

            let isInstalled = UIApplication.sharedApplication().canOpenURL(url)

            if isInstalled {

                print("Installed")

            }else{

                 print(" Not Installed")
            }

答案 2 :(得分:3)

还可以通过调用以下方式显式初始化工具包: com.sun.javafx.application.PlatformImpl#startup(Runnable)

由于使用了* Impl,有点hacky,但如果您因某些原因不想使用ApplicationJXFPanel,这很有用。

答案 3 :(得分:0)

请参阅http://www.programcreek.com/java-api-examples/index.php?api=com.sun.javafx.application.PlatformImpl

com.sun.javafx.application.PlatformImpl.startup(()->{});