java - Instantiating a web view media player within a JavaFX application -
i writing piece of educational software, , want user able select video watch list lies within javafx gui.
i have created mediaplayer class contains main method runs code , displays video correctly, next task instantiate mediaplayer class, , pass url of video watched parameter.
i have attempted code mediaplayertest, whereby instantiate mediaplayer, pass url parameter, , call start() method, getting following errors when running tester class:
exception in thread "main" java.lang.exceptionininitializererror @ javafx.scene.web.webview.<init>(webview.java:271) @ media.mediaplayer.start(mediaplayer.java:34) @ media.mediaplayertest.main(mediaplayertest.java:23) caused by: java.lang.runtimeexception: internal graphics not initialized yet @ com.sun.glass.ui.screen.getscreens(screen.java:70) @ com.sun.javafx.webkit.prism.prismgraphicsmanager.<init>(prismgraphicsmanager.java:43) @ javafx.scene.web.webengine.<clinit>(webengine.java:290) ... 3 more
any solving issue appreciated, can find tester class , 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); } // url of video 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(); } }
mediaplayertest
package media; import java.util.logging.level; import java.util.logging.logger; /** * * @author joe */ 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); } } }
you should not invoke start()
yourself: invoked part of complex process when call launch()
.
so 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); } // url of video 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 (if want, typically not 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 mediaplayertest
class redundant anyway: can run mediaplayer
class directly.
the bottom line application
subclasses represent entry point of application, , such not designed reusable. if want reuse mediaplayer
class elsewhere, refactor it's not application
subclass. see java: how start standalone application current 1 when both in same package?
Comments
Post a Comment