freshwaterjoe February 2016

Instantiating a web view media player within a JavaFX application

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

Any help for solving this issue would be much appreciated, you can find the tester class and mediaPlayer class code below:

mediaPlayer

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class mediaPlayer extends Application {   



  public static void main(String[] args) { launch(args); }


  // 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.sho        

Answers


James_D February 2016

You should not invoke start() yourself: it is invoked as part of a complex process when you call launch().

So you can do:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import java.util.List ;

public class MediaPlayer extends Application {   



  public static void main(String[] args) { launch(args); }


  // The url of the video to be played
  private String url = "https://www.youtube.com/embed/CySfQY_lgr4";


  @Override public void start(Stage stage) throws Exception {

    List<String> params = getParameters.getRaw();
    if (params.size() > 0) {
        url = params.get(0);
    }

    WebView webview = new WebView();
    webview.getEngine().load(url);
    webview.setPrefSize(640, 390);

    stage.setScene(new Scene(webview));
    stage.show();
  }    
}

and then (if you want, but typically this is not a good approach):

import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Joe
 */
public class MediaPlayerTest {

    public static void main (String[] args) {

        try {
            Application.launch(MediaPlayer.class, "https://www.youtube.com/embed/CySfQY_lgr4");
        } catch (Exception ex) {
            Logger.getLogger(MediaPlayerTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

Obviously here the MediaPlayerTest class is somewhat redundant anyway: you can just run the MediaPlayer class directly.

The bottom line is that Application subclasses represent the entry point of the application, and as such they are not designed to be reusable. If you want to reuse your MediaPlayer class elsewhere, then refactor it so it's not an Application subclass. See

Post Status

Asked in February 2016
Viewed 3,954 times
Voted 5
Answered 1 times

Search




Leave an answer