-1

I have a media player and i am putting a listiner on my media player for both the slider value and the time value my question is how do i format the time that i am putting in the label to display as 0:00 the first 0 being the minute the second being 10s of seconds and the third every one second.

public void setUpsongDurationSlider()
{
    musicMedia.getMediaPlayer().currentTimeProperty().addListener((obs, oldTme, newTime)->
    {
        homeView.getSongDurationSlider().setValue(newTime.toSeconds());
        homeView.getSongDurationSliderLabel().setText(Double.toString((newTime.toMinutes())));
    });
}
Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Bobby Gannard
  • 65
  • 1
  • 8

2 Answers2

0

Simply retrieve the minutes/seconds from the Duration object and use String.format to pad the seconds...

The following example only prints to System.out and uses a property created in the code, but you should be able to adjust it for your purpose:

ObjectProperty<Duration> duration = new SimpleObjectProperty(Duration.ZERO);

duration.addListener((observable, oldValue, newValue) -> {
    System.out.println(String.format("%d:%02d",
            (long)newValue.toMinutes(),
            ((long) newValue.toSeconds()) % 60));
});

// test for 1-99 seconds
for (int i = 1; i < 100; i++) {
    duration.set(Duration.seconds(i));
}
fabian
  • 80,457
  • 12
  • 86
  • 114
-1

so that other post helped but it is a double value that needed to be converted so i needed to use "%f" instead of "%d".

musicMedia.getMediaPlayer().currentTimeProperty().addListener((obs, oldTme, newTime)->
        {
            homeView.getSongDurationSlider().setValue(newTime.toSeconds());
            homeView.getSongDurationSliderLabel().setText(String.format("%.2f min",(newTime.toMinutes())));
Bobby Gannard
  • 65
  • 1
  • 8