在JavaFX应用程序中实例化Web视图媒体播放器 [英] Instantiating a web view media player within a JavaFX application
问题描述
我正在编写一个教育软件,并希望用户能够从我的JavaFX GUI中的列表中选择要观看的视频。
我创建了一个 mediaPlayer
类,其中包含一个运行代码并正确显示视频的main方法,
但是我的下一个任务是实例化 mediaPlayer
class,并将要观看的视频的网址作为参数传递。
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.
我试图编码a MediaPlayerTest
,我实例化 mediaPlayer
,将URL作为参数传递给它,然后调用 start()
方法,
但是我在运行测试人员课程时遇到以下错误:
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
你可以找到测试者类和 mediaPlayer
以下类代码:
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);
}
}
和 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.
推荐答案
你不应该调用 start()
您自己:当您调用 launch()
时,它会作为复杂过程的一部分被调用。
You should not invoke start()
yourself: it is invoked as part of a complex process when you call launch()
.
所以你可以这样做:
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);
}
}
}
显然这里无论如何, MediaPlayerTest
类有点多余:您可以直接运行 MediaPlayer
类。
Obviously here the MediaPlayerTest
class is somewhat redundant anyway: you can just run the MediaPlayer
class directly.
底线是 Application
子类代表应用程序的入口点,因此它们不是可重用的。如果你想在其他地方重用你的 MediaPlayer
类,那么重构它,使它不是 Application
子类。请参阅 Java :当两个应用程序位于同一个程序包中时,如何从当前应用程序启动独立应用程序?
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 Java: How do I start a standalone application from the current one when both are in the same package?
这篇关于在JavaFX应用程序中实例化Web视图媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!