Java Reference
In-Depth Information
lambda expression is called on the JavaFX application thread. Inside the
run
body of the lambda expression we
get the
currentTime
from the
MediaPlayer
and pass it as a parameter to the
updatePositionSlider
method. The
updatePositionSlider
method, also shown in Listing 9-19, first checks to see if the
positionSlider
's value is
currently changing and, if so, returns without doing anything. We do this to make sure we don't change the slider's
position while the user is currently dragging it. Doing so would make the slider's thumb bar flicker back and forth
between the drag position and the position indicated by the
currentTime
value. Needless to say, this is an unwanted
effect that we are careful to avoid. If the user is not currently changing the position slider's value, we are free to update
it based on the value of
currentTime
and
totalDuration
. The position slider is created with minimum and maximum
values of 0.0 and 1.0. The
currentTime
value is therefore divided by the media's
totalDuration
to calculate the
correct value for the slider. If either
totalDuration
or
currentValue
is null, the slider is simply positioned at 0.
You probably noticed that the
CurrentTimeListener
also formats the value of
currentTime
and displays it as
the text of the
currentTimeLabel
. If you refer back to Figure
9-5
you can see that the position slider is bracketed by
two
Label
controls. On the left is a label that displays the
currentTime
value, and the
totalDuration
is displayed
by a label to the right of the slider. The label displaying the
totalDuration
is set by a listener that is attached to
MediaPlayer
's
totalDuration
property much like the
CurrentTimeListener
shown earlier. Whenever the value of
totalDuration
changes, the listener calls the
formatDuration
method to create a
String
to use as the label's text. The
formatDuration
method used to format both of these values is shown in Listing 9-20.
Listing 9-20.
The
formatDuration
Method
private String formatDuration(Duration duration) {
double millis = duration.toMillis();
int seconds = (int) (millis / 1000) % 60;
int minutes = (int) (millis / (1000 * 60));
return String.format("%02d:%02d", minutes, seconds);
}
The
formatDuration
method takes a
Duration
as a parameter and returns a
String
with the format
mm
:
ss
where
mm
is minutes and
ss
is seconds. The minutes and seconds values will be zero-padded if they are only one digit long.
The second part of the position slider's job is to allow the user to set the playback position by dragging the slider
back and forth. This is accomplished by listening for changes to the
Slider
's
valueChangingProperty
as shown in
Listing 9-21.
Listing 9-21.
Listening to Position Slider Changes and Seeking
@Override
protected Node initView() {
// Controls are created as shown in Listing 9-15.
positionSlider = createSlider("positionSlider");
positionSlider.valueChangingProperty().addListener(new PositionListener());
// Lay out the GridPane as shown in Listing 9-15.
}
private class PositionListener implements ChangeListener<Boolean> {
@Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
if (oldValue && !newValue) {
double pos = positionSlider.getValue();
final MediaPlayer mediaPlayer = songModel.getMediaPlayer();
Search WWH ::
Custom Search