簡體   English   中英

帶有JavaFX的Musicplayer-如何獲取文件名作為歌曲名?

[英]Musicplayer with JavaFX - How can I get the filename as song name?

我是編碼方面的新手,目前正在嘗試使用JavaFX對音樂播放器進行編碼。 我正在使用Eclipse和Scene Builder,目前正在嘗試在songName label上獲取文件名。 但是,啟動程序時,文件名未顯示在標簽中。 我的錯誤在哪里,您還看到其他錯誤或改進之處嗎?

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) {

}

您絕不會嘗試將歌曲的名稱設置到Label 您具有nameSong()方法,但是在您的代碼中從未調用過它。

當您要更新Label時,只需在某個時候調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM