i .
i .

Reputation: 485

javafx error in MediaPlayer constructor IllegalStateException

I have a pretty simple code that I got from this site (courtesy of senderle) to play an mp3 file (I am new with 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();
    }
}

when I run this code, I get the following exception:

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)

How do I fix this?

Upvotes: 1

Views: 1500

Answers (1)

jewelsea
jewelsea

Reputation: 159341

Use your media in one of:

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

Any of the above options will ensure that the JavaFX toolkit is appropriately initialized before you start to use components that depend on it.

Upvotes: 1

Related Questions