0

有谁知道如何在 FXML 中嵌入媒体播放器来打开 youtube 视频?

我写了这个:

<?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>

但是我在声明“媒体”的行中遇到了这个异常:

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)
   ...  

任何人 ?

谢谢你们 !

4

2 回答 2

2

出于版权原因,AFAIK YouTube 不允许流式传输他的视频。

1.但是你可以在 JavaFX 的 WebView 中嵌入 youtube 视频:

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.不幸的是,与 FXML 相同的事情需要使用 Controller,因为我不知道如何从 FXML 为 WebEngine 设置内容:

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

和控制器:

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' />");
    }

}
于 2012-08-28T12:19:52.317 回答
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
于 2013-06-13T22:26:03.547 回答