我从这个网站获得了一个非常简单的代码(由 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)
我该如何解决这个问题?
最佳答案
将您的媒体用于以下用途之一:
- JavaFX Application
- Swing JFXPanel
- SWT FXCanvas
以上任何选项都将确保 JavaFX 工具包在您开始使用依赖于它的组件之前得到适当的初始化。
关于MediaPlayer 构造函数中的 javafx 错误 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779050/