MediaPlayer 构造函数中的 javafx 错误 IllegalStateException

标签 java media-player javafx illegalstateexception

我从这个网站获得了一个非常简单的代码(由 senderle 提供)来播放 mp3 文件(我是 javafx 的新手)

import javafx.scene.media.*;
public class Start
{
    public static void main(String[] args)
    {
        String bip = "file:/Sounds/sound.mp3";
        Media hit = new Media(bip);
        MediaPlayer mediaPlayer = new MediaPlayer(hit); // This is line 8 from the Exception
        mediaPlayer.play();
    }
}

当我运行此代码时,出现以下异常:

Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:155)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:150)
    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 Start.main(Start.java:8)

我该如何解决这个问题?

最佳答案

将您的媒体用于以下用途之一:

  1. JavaFX Application
  2. Swing JFXPanel
  3. SWT FXCanvas

以上任何选项都将确保 JavaFX 工具包在您开始使用依赖于它的组件之前得到适当的初始化。

关于MediaPlayer 构造函数中的 javafx 错误 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779050/

相关文章:

Android 帮助 : How do open a remote Video file URL to play in MediaPlayer without having to open a browser window?

JavaFX 和多线程 : IllegalStateException on ObservableList. add()

从 java 7u21 迁移到更高版本时,Javafx 应用程序无法启动

java - Weblogic服务器性能低下

java.util.NoSuchElementException - 扫描仪读取用户输入

ffmpeg - 通过ffmpeg反向播放视频

java - 在javafx中用按钮覆盖标签背景

java - 在 Netbeans 向导中使用 JFrame 而不是 JDialog

java - 如何开发 IMAP 服务器接口(interface),有可用的库吗?

ios - AV Foundation框架和Media Player框架的区别