我正在使用Netbeans IDE中的JavaFx构建视频播放器。我需要设置下面屏幕构建器中显示的标签,以显示视频的总持续时间。
当我使用System.out.println(""+mediaPlayer.getDuration.toSeconds());
时,我会以输出形式获得结果。但是,当我使用label.setText(""+mediaPlayer.getDuration.toSeconds());
时,我得到一个NullPointerException。
如何在标签中显示视频的总持续时间?
我的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="<<<">
<HBox.margin>
<Insets left="30.0" />
</HBox.margin></Button>
<Button fx:id="slowButton" mnemonicParsing="false" onAction="#slowVideo" text="<<" />
<Button fx:id="fastButton" mnemonicParsing="false" onAction="#fastVideo" text=">>" />
<Button fx:id="fasterButton" mnemonicParsing="false" onAction="#fasterVideo" text=">>>" />
<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>
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
我在FXML文档中只发现了一个Label
元素,但它没有fx:id
属性。这意味着它永远不会注入您的控制器。
根据您的控制器类,将fx:id="label"
作为属性添加到<Label text="Label">
元素。
为了将来参考,由于两行代码之间的主要区别在于使用label
字段,因此label
字段是个问题。由于对于NullPointerException
注入字段是FXML
,因为FXML文档和控制器类没有正确配置/编码,所以不注入该字段是个不错的选择。
这是可以真正偷偷摸摸的简单事情。