The problem is because you are trying to run JavaFX scene graph control outside of JavaFX Application thread
.
Run all JavaFX scene graph nodes inside the JavaFX application thread.
You can start a JavaFX thread by extending JavaFX Application
class and overriding the start()
method.
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Media pick = new Media("put.mp3"); // replace this with your own audio file
MediaPlayer player = new MediaPlayer(pick);
// Add a mediaView, to display the media. Its necessary !
// This mediaView is added to a Pane
MediaView mediaView = new MediaView(player);
// Add to scene
Group root = new Group(mediaView);
Scene scene = new Scene(root, 500, 200);
// Show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
// Play the media once the stage is shown
player.play();
}
public static void main(String[] args) {
launch(args);
}
}