0

Does anyone know how to embed a media player in FXML to open a youtube video ?

I wrote this :

<?import javafx....>
<?import javafx.scene.media.Media?>
<?import javafx.scene.media.MediaPlayer?>
<?import javafx.scene.media.MediaView?>

<AnchorPane prefHeight="600" VBox.vgrow="ALWAYS">
  <children>
    <VBox spacing="50" alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <padding>
        <Insets top="30" right="30" bottom="0" left="30"/>
      </padding>
      <children>
        <MediaView>
          <MediaPlayer autoPlay="true">
            <Media source="http://www.youtube.com/watch?v=rCw5JXD18y4" />
          </MediaPlayer>
        </MediaView>
      </children>
    </VBox>
  </children>
</AnchorPane>

But I am getting this exception in the line where "Media" is declared :

Element does not define a default property.
   at javafx.fxml.FXMLLoader$Element.set(FXMLLoader.java:142)
   at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:611)
   at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
   at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:937)
   at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:567)
   at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)
   ...  

Anyone ?

Thank you all !

2 Answers 2

2

AFAIK YouTube doesn't allow to stream his videos for copyright reasons.

1. But you can embed youtube video in WebView in JavaFX:

public class WebViewYouTube extends Application {
    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        WebView webView = new WebView();

        final WebEngine webEngine = webView.getEngine();
        root.getChildren().addAll(webView);
        stage.setScene(new Scene(root, 450, 350));

        webEngine.loadContent("<iframe width='420' height='315' src='http://www.youtube.com/embed/ZDzVLhjB3bk' frameborder='0' allowfullscreen></iframe>");

        stage.show();
    }

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

2. Same thing with FXML unfortunately requires using Controller as I don't know a way to set content for WebEngine from FXML:

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxdemo.SampleController">
  <children>
    <WebView fx:id="webView"/>
  </children>
</StackPane>

and controller:

public class SampleController implements Initializable {

    @FXML //  fx:id="webView"
    private WebView webView; // Value injected by FXMLLoader


    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        webView.getEngine().loadContent("<iframe width='420' height='315' src='http://www.youtube.com/embed/ZDzVLhjB3bk' />");
    }

}
3
  • Any ideas on how to implement this in FXML? Thanks Sergey ! Aug 28, 2012 at 12:42
  • Only way I see is one described in the 2nd part of the answer Aug 28, 2012 at 13:01
  • For me the 2nd part doesn't work. The project runs but nothing is loaded in the webView.
    – dyllandry
    Jul 5, 2016 at 19:29
0
    I have a class to play video

    @Override public void start(Stage primaryStage) {
        WebView root = new WebView();
        root.getEngine().loadContent(
          "<video width='640' height='480'controls='controls'>" +
          "<source src='http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv'/>" + 
          "</video>");

        primaryStage.setScene(new Scene(root, 660, 500));
        primaryStage.show();`enter code here`
    }

but I need to know if you can linkear an online player, as a transmission of a party or a live event Ej: A transmission justin.tv or a game of www.rojadirecta.mx, as I can know what kind of content is and to load it into my webview?
    Thanks
1
  • please translate your text parts into English ... it doesn't need to be perfect ... and users aren't supposed to put you stuff into google translate & co.
    – kiwiwings
    Jun 13, 2013 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.