视频播放器

Video Player JavaFx

我正在 Netbeans 中使用 JavaFx 构建视频播放器 IDE。我需要设置下面屏幕构建器中显示的标签以显示视频的总持续时间。

当我使用 System.out.println(""+mediaPlayer.getDuration.toSeconds()); 时,我得到了输出形式的结果。但是,当我使用 label.setText(""+mediaPlayer.getDuration.toSeconds()); 时,我得到一个 NullPointerException。

如何在标签中显示视频的总时长?[​​=14=]

我的FXMLDocumentController.java代码

public class FXMLDocumentController implements Initializable {

private MediaPlayer mediaPlayer; 
private String filePath;

@FXML
private Label label;

@FXML
private Slider seek;

@FXML
private Slider volume;

@FXML
private MediaView mediaView;

@FXML
private void handleButtonAction(ActionEvent event) throws InterruptedException {



    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select File (*.mp4)", "*.mp4");
        fileChooser.getExtensionFilters().add(filter);
        File file = fileChooser.showOpenDialog(null);
        filePath = file.toURI().toString();

        if(filePath!=null)
        {
            Media media = new Media(filePath);
            mediaPlayer = new MediaPlayer(media);


            mediaView.setMediaPlayer(mediaPlayer);

                DoubleProperty width = mediaView.fitWidthProperty();
                DoubleProperty height = mediaView.fitHeightProperty();

                width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
                height.bind(Bindings.selectDouble(mediaView.sceneProperty(),"height"));

                volume.setValue(mediaPlayer.getVolume() * 100);
                volume.valueProperty().addListener(new InvalidationListener() {
                @Override
                public void invalidated(Observable observable) {
                   mediaPlayer.setVolume(volume.getValue() / 100);
                }
            });

                mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
                @Override
                public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                    seek.setValue(newValue.toSeconds());
                }
            });

                seek.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    mediaPlayer.seek(Duration.seconds(seek.getValue()));
                }
            });
            mediaPlayer.setOnReady(new Runnable() {

            @Override
            public void run() 
                    {

                        label.setText("Duration: "+media.getDuration().toSeconds());
                       //Using Output -  System.out.println(""+mediaPlayer.getDuration.toSeconds());

                    }

                });
            mediaPlayer.play();
        }
}

@FXML
private void playVideo(ActionEvent event)
{
    mediaPlayer.play();
    mediaPlayer.setRate(1);
}

@FXML
private void pauseVideo(ActionEvent event)
{
    mediaPlayer.pause();
}

@FXML
private void stopVideo(ActionEvent event)
{
    mediaPlayer.stop();
}

@FXML
private void fastVideo(ActionEvent event)
{
    mediaPlayer.setRate(1.5);
}

@FXML
private void fasterVideo(ActionEvent event)
{
    mediaPlayer.setRate(2);
}

@FXML
private void slowVideo(ActionEvent event)
{
    mediaPlayer.setRate(0.75);
}

@FXML
private void slowerVideo(ActionEvent event)
{
    mediaPlayer.setRate(0.5);
}

@FXML
private void exitVideo(ActionEvent event)
{
     System.exit(0);
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    
}

我的FXMLDocument.fxml代码

 <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bideo.player.FXMLDocumentController">
   <bottom>
      <VBox alignment="CENTER" prefHeight="40.0" prefWidth="600.0" BorderPane.alignment="CENTER">
         <children>
            <HBox alignment="CENTER" prefHeight="40.0">
               <children>
                  <Button fx:id="openFile" minHeight="32.0" minWidth="32.0" mnemonicParsing="false" onAction="#handleButtonAction">
                     <HBox.margin>
                        <Insets left="10.0" />
                     </HBox.margin></Button>
                  <Button fx:id="playButton" minHeight="32.0" minWidth="32.0" mnemonicParsing="false" onAction="#playVideo">
                     <HBox.margin>
                        <Insets left="30.0" />
                     </HBox.margin></Button>
                  <Button fx:id="pauseButton" minHeight="32.0" minWidth="32.0" mnemonicParsing="false" onAction="#pauseVideo">
                     <HBox.margin>
                        <Insets left="5.0" />
                     </HBox.margin></Button>
                  <Button fx:id="stopButton" minHeight="32.0" minWidth="32.0" mnemonicParsing="false" onAction="#stopVideo">
                     <HBox.margin>
                        <Insets left="5.0" />
                     </HBox.margin></Button>
                  <Button fx:id="slowerButton" mnemonicParsing="false" onAction="#slowerVideo" text="&lt;&lt;&lt;">
                     <HBox.margin>
                        <Insets left="30.0" />
                     </HBox.margin></Button>
                  <Button fx:id="slowButton" mnemonicParsing="false" onAction="#slowVideo" text="&lt;&lt;" />
                  <Button fx:id="fastButton" mnemonicParsing="false" onAction="#fastVideo" text="&gt;&gt;" />
                  <Button fx:id="fasterButton" mnemonicParsing="false" onAction="#fasterVideo" text="&gt;&gt;&gt;" />
                  <Button mnemonicParsing="false" onAction="#exitVideo" text="Exit" />
                  <Slider fx:id="volume" prefHeight="14.0" prefWidth="72.0">
                     <HBox.margin>
                        <Insets left="20.0" />
                     </HBox.margin>
                  </Slider>
                  <Label text="Label">
                     <HBox.margin>
                        <Insets left="15.0" />
                     </HBox.margin>
                  </Label>
               </children>
            </HBox>
         </children>
      </VBox>
   </bottom>
   <center>
      <StackPane prefHeight="150.0" prefWidth="200.0">
         <children>
            <MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" StackPane.alignment="CENTER" />
            <Slider fx:id="seek" StackPane.alignment="BOTTOM_CENTER" />
         </children>
      </StackPane>
   </center>
</BorderPane>

如有任何帮助,我们将不胜感激。谢谢

我只在您的 FXML 文档中发现了一个 Label 元素,它没有 fx:id 属性。这意味着它永远不会注入到您的控制器中。

根据您的控制器 class,将 fx:id="label" 作为属性添加到 <Label text="Label"> 元素。

供将来参考,因为两行代码之间的主要区别在于 label 字段的使用,所以可以肯定 label 字段是问题所在。并且由于它是关于 FXML 注入字段的 NullPointerException ,因此可以肯定该字段未注入是由于 FXML 文档和控制器 class 未被正确 configured/coded .

真正让你着迷的是简单的事情。