When i create new stage with WebEngine that playing video from YouTube, after i close it - Youtube keeps playing on backgroung. If i use "Platform.exit" - its close all my JavaFX App, but i want to close only stage that been created for YouTube.
This is my class for YouTube player:
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 )))
}
});
}
}
My Youtube player stage is creating after i clicking on button in 'mainstage':
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
new YouTube_player(url_video_p1);
}
});
You cant dispose the webengine the only thing that you can do is to set the content of the webEngine to
null
webView.getEngine().load(null);
Java 9, for me works: