简体   繁体   English

MediaView JavaFX FXML上的按钮

[英]Button on MediaView JavaFX FXML

How would I go about creating a button on my MediaView component, such as a play button in the centre of the video that disapears when clicked. 我该如何在MediaView组件上创建一个按钮,例如在视频中心单击一个播放按钮,该按钮会消失。 I am working with FXML so if you could give me an answer using that it would be even better. 我正在使用FXML,所以如果您可以使用它给我一个答案,那就更好了。

You can easily add one Button on top of the media player, by having both on a StackPane . 您可以通过在StackPane上轻松将一个Button添加到媒体播放器的顶部。

Then add a method to the button, so when it's clicked the button it's hidden and the video starts playing. 然后向该按钮添加一个方法,因此单击该按钮时将其隐藏,并且视频开始播放。

I'll add also a way to show again the button once the video ends, letting you start again. 我还将添加一种在视频结束后再次显示按钮的方法,让您重新开始。

This is the FXML: 这是FXML:

<AnchorPane prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
    <children>
        <StackPane>
           <children>
              <MediaView>
                  <mediaPlayer>
                      <MediaPlayer fx:id="mediaPlayer" autoPlay="false">
                          <media>
                              <Media source="http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv" />
                          </media>
                      </MediaPlayer>
                  </mediaPlayer>    
              </MediaView>
              <Button fx:id="button" mnemonicParsing="false" onAction="#playAndHide" text="Play Video" />
           </children>
        </StackPane>
    </children>
</AnchorPane>

and this is the Controller class: 这是Controller类:

@FXML private Button button;
@FXML private MediaPlayer mediaPlayer;

@Override
public void initialize(URL url, ResourceBundle rb) {
    mediaPlayer.setOnEndOfMedia(()->button.setVisible(true));
}    

@FXML
public void playAndHide(ActionEvent event){
    button.setVisible(false);
    mediaPlayer.seek(Duration.ZERO);
    mediaPlayer.play();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM