Matheus
Matheus

Reputation: 11

JavaFX 8 Media Player Progress Slider

So, I have been developing a Media Player application using JavaFX for some time. Unfortunately I have come accross a problem which I don't know how to fix. The slider in my GUI that is supposed to show the video's progress is not working properly. It ends too early. So the video continues until it ends, but the slider has already stopped.

Any help would be greatly appreciated.

Double time = player.getTotalDuration().toSeconds();

    player.currentTimeProperty().addListener((ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) -> {
        slider.setValue(newValue.toSeconds());
    });
    slider.setOnMouseClicked((MouseEvent mouseEvent) -> {
        player.seek(Duration.seconds(slider.getValue()));
    });

Upvotes: 1

Views: 3953

Answers (1)

James_D
James_D

Reputation: 209408

You need the max property of the slider to be equal to the total number of seconds of the media you are playing. Note that the totalDuration of the media won't be known until the media player has read enough information from the media resource. The best approach is to use a binding for this:

slider.maxProperty().bind(Bindings.createDoubleBinding(
    () -> player.getTotalDuration().toSeconds(),
    player.totalDurationProperty()));

In some cases, the media resource will not indicate the total duration; in this case it will probably be impossible to link a slider to the current time of the player, unless you have some other mechanism of knowing the total duration.

Upvotes: 2

Related Questions