Friday, March 22, 2013

Embed Youtube video in JavaFX WebView

The code embed Youtube view in JavaFX WebView.

Embed Youtube video in JavaFX WebView
Embed Youtube video in JavaFX WebView


package javafx_webviewyoutube;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_WebViewYoutube extends Application {

    String content_Url = "<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/9bZkp7q19f0\" frameborder=\"0\" allowfullscreen></iframe>";

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.loadContent(content_Url);

        StackPane root = new StackPane();
        root.getChildren().add(webView);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("http://java-buddy.blogspot.com/");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

To load the html code from YouTube, open a browser to load Youtube with the target video, select Share -> Embed to copy the html code into content_Url.


4 comments:

  1. Se queda cargando el video y no reproduce no saben cual sera el problema?
    Gracias

    ReplyDelete
  2. Thank you very much!!!!!!!!!! Great help!!!

    Youtube Help

    ReplyDelete

  3. Hi , I am using same code in our application but this code is not able to play youtube video. Application view is successfully open but after giving error message when its try to play. we are facing some this kind of error in the view section Error:- "An error occurred. Please try again later". So please let me what is the exact problem

    ReplyDelete
  4. I have above question too. So could you please tell me how to resolve that problem

    ReplyDelete