带JavaFX的Musicplayer-如何将文件名作为歌曲名?

带JavaFX的Musicplayer-如何将文件名作为歌曲名?,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我对编码非常陌生,目前我正在尝试使用JavaFX为音乐播放器编码。我正在使用Eclipse和Scene Builder,目前正在尝试获取songName标签上的文件名。但启动程序时,文件名不会显示在标签中。 我的错误在哪里?您是否看到其他错误或改进 public class MainController implements Initializable { @FXML private MediaPlayer mp; @FXML private Slider volumeSlider; @FX

我对编码非常陌生,目前我正在尝试使用JavaFX为音乐播放器编码。我正在使用Eclipse和Scene Builder,目前正在尝试获取songName
标签上的文件名。但启动程序时,文件名不会显示在标签中。
我的错误在哪里?您是否看到其他错误或改进

public class MainController implements Initializable {
@FXML
private MediaPlayer mp;


@FXML
private Slider volumeSlider;
@FXML
private String filePath;
@FXML
private Slider seekSlider;
private Label timeLabel;
private Duration duration;
private Label songName;



@FXML
private void handleButtonAction (ActionEvent event) 
{
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File (*.mp3)", "*.mp3");
        fileChooser.getExtensionFilters().add(filter);
        File file = fileChooser.showOpenDialog(null);
        filePath = file.toURI().toString();

        if(filePath != null) 
        {
            Media media = new Media(filePath);
            mp = new MediaPlayer(media);
            mp.play();


            volumeSlider.setValue(mp.getVolume() * 100);
            volumeSlider.valueProperty().addListener(new InvalidationListener() 
            {

            @Override
            public void invalidated(Observable observable) 
            {
            mp.setVolume(volumeSlider.getValue() / 200);

            }



            }); 


            mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {

                @Override
                public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue)
                {
                seekSlider.setValue(newValue.toSeconds());


                }

            });

            seekSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {

                    mp.seek(Duration.seconds(seekSlider.getValue()));

                }


            });


        }
}

public void nameSong() 
{
    songName.setText(filePath.toString());


}

        protected void updateValues() {
              if (timeLabel != null && seekSlider != null && volumeSlider != null) {
                 Platform.runLater(new Runnable() {
                    @SuppressWarnings("deprecation")
                    public void run() {
                      Duration currentTime = mp.getCurrentTime();
                      timeLabel.setText(formatTime(currentTime, duration));
                      seekSlider.setDisable(duration.isUnknown());
                      if (!seekSlider.isDisabled() 
                        && duration.greaterThan(Duration.ZERO) 
                        && !seekSlider.isValueChanging()) {
                          seekSlider.setValue(currentTime.divide(duration).toMillis()
                              * 100.0);
                      }
                      if (!volumeSlider.isValueChanging()) {
                        volumeSlider.setValue((int)Math.round(mp.getVolume() 
                              * 100));
                      }
                    }
                 });
              }





}

        private static String formatTime(Duration elapsed, Duration duration) {
               int intElapsed = (int)Math.floor(elapsed.toSeconds());
               int elapsedHours = intElapsed / (60 * 60);
               if (elapsedHours > 0) {
                   intElapsed -= elapsedHours * 60 * 60;
               }
               int elapsedMinutes = intElapsed / 60;
               int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 
                                       - elapsedMinutes * 60;

               if (duration.greaterThan(Duration.ZERO)) {
                  int intDuration = (int)Math.floor(duration.toSeconds());
                  int durationHours = intDuration / (60 * 60);
                  if (durationHours > 0) {
                     intDuration -= durationHours * 60 * 60;
                  }
                  int durationMinutes = intDuration / 60;
                  int durationSeconds = intDuration - durationHours * 60 * 60 - 
                      durationMinutes * 60;
                  if (durationHours > 0) {
                     return String.format("%d:%02d:%02d/%d:%02d:%02d", 
                        elapsedHours, elapsedMinutes, elapsedSeconds,
                        durationHours, durationMinutes, durationSeconds);
                  } else {
                      return String.format("%02d:%02d/%02d:%02d",
                        elapsedMinutes, elapsedSeconds,durationMinutes, 
                            durationSeconds);
                  }
                  } else {
                      if (elapsedHours > 0) {
                         return String.format("%d:%02d:%02d", elapsedHours, 
                                elapsedMinutes, elapsedSeconds);
                        } else {
                            return String.format("%02d:%02d",elapsedMinutes, 
                                elapsedSeconds);
                        }
                    }
                }





 @FXML
public void play(ActionEvent event) 
{
    mp.play();
    mp.setRate(1);

}
 @FXML
public void pause(ActionEvent event) 
{
    mp.pause();


}
 @FXML
public void stop(ActionEvent event) 
{
    mp.stop();


}
 @FXML
public void slow(ActionEvent event) 
{
    mp.setRate(0.5);


}
 @FXML
public void fast(ActionEvent event) 
{
    mp.setRate(2);


}
 @FXML
public void reload(ActionEvent event) 
{
    mp.seek(mp.getStartTime());
    mp.play();


}
 @FXML
public void next(ActionEvent event) 
{
    mp.seek(mp.getTotalDuration());
    mp.play();


}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {

}
public类MainController实现可初始化{
@FXML
私人媒体播放器;
@FXML
私人滑杆滑杆;
@FXML
私有字符串文件路径;
@FXML
私人滑杆式滑翔机;
私有标签;
私人期限;
自有品牌songName;
@FXML
私有无效把手按钮操作(ActionEvent事件)
{
FileChooser FileChooser=newfilechooser();
FileChooser.ExtensionFilter=newfilechooser.ExtensionFilter(“选择文件(*.mp3)”,“*.mp3”);
fileChooser.getExtensionFilters().add(过滤器);
File File=fileChooser.showOpenDialog(null);
filePath=file.toURI().toString();
if(filePath!=null)
{
媒体=新媒体(文件路径);
mp=新媒体播放器(媒体);
下院议员play();
volumeSlider.setValue(mp.getVolume()*100);
volumeSlider.valueProperty().addListener(新的InvalizationListener())
{
@凌驾
公共无效(可观察到)
{
mp.setVolume(volumeSlider.getValue()/200);
}
}); 
mp.currentTimeProperty().addListener(新的ChangeListener()){
@凌驾

public void changed(observeValue您从未尝试将歌曲名称设置到
标签中。您有
nameSong()
方法,但从未在代码中调用它

您只需要在希望更新
标签时调用它