gpt4 book ai didi

MediaPlayer 构造函数中的 javafx 错误 IllegalStateException

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:56 25 4
gpt4 key购买 nike

我从这个网站获得了一个非常简单的代码(由 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com