I am writing a piece of educational software, and want the user to be able to select a video to watch from a list that lies within my JavaFX GUI.
I have created a mediaPlayer
class that contains a main method that runs the code and displays the video correctly,
however my next task is to instantiate the mediaPlayer
class, and pass the URL of the video to be watched as a parameter.
I have attempted to code a MediaPlayerTest
, whereby I instantiate the mediaPlayer
, pass it a URL as a parameter, and then call the start()
method,
however I am getting the following errors when running my tester class:
Exception in thread "main" java.lang.ExceptionInInitializerError
at javafx.scene.web.WebView.<init>(WebView.java:271)
at Media.mediaPlayer.start(mediaPlayer.java:34)
at Media.MediaPlayerTest.main(MediaPlayerTest.java:23)
Caused by: java.lang.RuntimeException: Internal graphics not initialized yet
at com.sun.glass.ui.Screen.getScreens(Screen.java:70)
at com.sun.javafx.webkit.prism.PrismGraphicsManager.<init>(PrismGraphicsManager.java:43)
at javafx.scene.web.WebEngine.<clinit>(WebEngine.java:290)
... 3 more
You can find the tester class and mediaPlayer
class code below:
public class mediaPlayer extends Application {
// The url of the video to be played
String url = "https://www.youtube.com/embed/CySfQY_lgr4";
public mediaPlayer(String url) {
this.url = url;
}
@Override
public void start(Stage stage) throws Exception {
WebView webview = new WebView();
webview.getEngine().load(url);
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And MediaPlayerTest
class:
public class MediaPlayerTest {
public static void main (String[] args) {
try {
mediaPlayer mp = new mediaPlayer("https://www.youtube.com/embed/CySfQY_lgr4");
mp.start(null);
} catch (Exception ex) {
Logger.getLogger(MediaPlayerTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Any help for solving this issue would be much appreciated.