向JavaFX MediaPlayer.Status添加更改侦听器,为什么getStatus()方法会出现故障?

向JavaFX MediaPlayer.Status添加更改侦听器,为什么getStatus()方法会出现故障?,java,javafx,javafx-8,Java,Javafx,Javafx 8,我想观察myMediaPlayer.Status的值,以便出于控制原因使用它: 导入javafx.application.application; 导入javafx.beans.property.*; 导入javafx.event.ActionEvent; 导入javafx.scene.Group; 导入javafx.scene.scene; 导入javafx.scene.control.Button; 导入javafx.scene.control.Label; 导入javafx.scene.m

我想观察my
MediaPlayer.Status的值,以便出于控制原因使用它:

导入javafx.application.application;
导入javafx.beans.property.*;
导入javafx.event.ActionEvent;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.media.media;
导入javafx.scene.media.MediaPlayer;
导入javafx.stage.stage;
导入java.io.File;
公共类主扩展应用程序{
公共静态void main(字符串[]args){
应用程序启动(args);
}
公共无效开始(阶段primaryStage){
组=新组();
场景=新场景(组,250,50);
File File=新文件(“C:/Example.mp3”);
媒体=新媒体(文件.toURI().toString());
MediaPlayer=新的MediaPlayer(媒体);
按钮播放按钮=新按钮(“播放”);
按钮暂停按钮=新按钮(“暂停”);
按钮停止按钮=新按钮(“停止”);
标签信息标签=新标签(“信息”);
playButton.setLayoutX(0);
playButton.setLayoutY(0);
pauseButton.setLayoutX(50);
pauseButton.setLayoutY(0);
stopButton.setLayoutX(100);
stopButton.setLayoutY(0);
infoLabel.setLayoutX(150);
infoLabel.setLayoutY(0);
ObjectProperty info=新的SimpleObjectProperty(mediaPlayer);
info.addListener((可观察、旧值、新值)->infoLabel.setText(newValue.getStatus().toString());
playButton.addEventHandler(ActionEvent.ACTION,e->{
mediaPlayer.play();
});
pauseButton.addEventHandler(ActionEvent.ACTION,e->{
mediaPlayer.pause();
});
stopButton.addEventHandler(ActionEvent.ACTION,e->{
mediaPlayer.stop();
});
group.getChildren().addAll(播放按钮、暂停按钮、停止按钮、信息标签);
初级阶段。场景(场景);
primaryStage.show();
}
}
不幸的是,我似乎无法获得任何更改,获得结果的唯一方法是每次单击按钮时调用
mediaPlayer.getStatus()

但即便如此,它也需要点击两下才能正确更新状态

导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.media.media;
导入javafx.scene.media.MediaPlayer;
导入javafx.stage.stage;
导入java.io.File;
公共类主扩展应用程序{
公共静态void main(字符串[]args){
应用程序启动(args);
}
公共无效开始(阶段primaryStage){
组=新组();
场景=新场景(组,250,50);
File File=新文件(“C:/Example.mp3”);
媒体=新媒体(文件.toURI().toString());
MediaPlayer=新的MediaPlayer(媒体);
按钮播放按钮=新按钮(“播放”);
按钮暂停按钮=新按钮(“暂停”);
按钮停止按钮=新按钮(“停止”);
标签信息标签=新标签(“信息”);
playButton.setLayoutX(0);
playButton.setLayoutY(0);
pauseButton.setLayoutX(50);
pauseButton.setLayoutY(0);
stopButton.setLayoutX(100);
stopButton.setLayoutY(0);
infoLabel.setLayoutX(150);
infoLabel.setLayoutY(0);
playButton.addEventHandler(ActionEvent.ACTION,e->{
mediaPlayer.play();
infoLabel.setText(mediaPlayer.getStatus().toString());
});
pauseButton.addEventHandler(ActionEvent.ACTION,e->{
mediaPlayer.pause();
infoLabel.setText(mediaPlayer.getStatus().toString());
});
stopButton.addEventHandler(ActionEvent.ACTION,e->{
mediaPlayer.stop();
infoLabel.setText(mediaPlayer.getStatus().toString());
});
group.getChildren().addAll(播放按钮、暂停按钮、停止按钮、信息标签);
初级阶段。场景(场景);
primaryStage.show();
}
}

您应该能够访问status属性,如下所示:

mediaPlayer.statusProperty().addListener((observable, oldValue, newValue) -> infoLabel.setText(newValue.toString()));

您应该能够访问status属性,如下所示:

mediaPlayer.statusProperty().addListener((observable, oldValue, newValue) -> infoLabel.setText(newValue.toString()));