当我使用WebEngine创建一个从YouTube播放视频的新舞台后,关闭它后YouTube仍然在后台播放。如果我使用“Platform.exit”,它将关闭所有JavaFX应用程序,但我只想关闭为YouTube创建的舞台。
这是我的YouTube播放器类:
public class YouTube_player {
public YouTube_player(String url) {
final Group root = new Group();
Scene scene = new Scene(root, 820, 480);
final Stage stage = new Stage();
final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
webEngine.loadContent(url);
root.getChildren().add(webView);
stage.centerOnScreen();
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>(){
@Override
public void handle(WindowEvent event) {
//What i should put here to close only this stage.
//Platform.exit - closes all my stages.
//webEngine.getLoadWorker().cancel(); - dont stop Youtube )))
}
});
}
}
当我在'mainstage'中点击按钮后,我的Youtube播放器舞台被创建:
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
new YouTube_player(url_video_p1);
}
});
Platform.exit()
不起作用,因为它会关闭整个子系统。所以当你仍然想在其他窗口/视图中使用它时,就不能这样做。没有重新启动 JVM,无法从Platform.exit()
中恢复。 - Frederic LeitenbergerCountdownLatch
如何停止 JS 脚本(这是 VM 代码)。至于“杀死循环” - 我也不知道怎么做...我所知道的唯一方法是中断或终止线程,但这可能也不可行,或者会导致其他不良副作用。 - Frederic Leitenberger