Java Reference
In-Depth Information
seekStartButton.setOnAction(event -> {
seekAndUpdatePosition(Duration.ZERO);
});
final Button seekEndButton = new Button();
seekEndButton.setId("seekEndButton");
seekEndButton.setOnAction(event -> {
final MediaPlayer mediaPlayer = songModel.getMediaPlayer();
final Duration totalDuration = mediaPlayer.getTotalDuration();
final Duration oneSecond = Duration.seconds(1);
seekAndUpdatePosition(totalDuration.subtract(oneSecond));
});
hbox.getChildren().addAll(seekStartButton, playPauseButton, seekEndButton);
return hbox;
}
private void seekAndUpdatePosition(Duration duration) {
final MediaPlayer mediaPlayer = songModel.getMediaPlayer();
if (mediaPlayer.getStatus() == Status.STOPPED) {
mediaPlayer.pause();
}
mediaPlayer.seek(duration);
if (mediaPlayer.getStatus() != Status.PLAYING) {
updatePositionSlider(duration);
}
}
Recall that Listing 9-16 showed the code for the createPlayPauseButton method. Listing 9-18 shows the creation
of the rest of the playback control panel, namely the seekStartButton and the seekEndButton . These two buttons
specify a seek time as the argument to the seekAndUpdatePosition helper method. For the seekStartButton , the
seek time is Duration.ZERO , which will always seek to the start of the media. The seekEndButton actually seeks to one
second prior to the end of the media, which is specified by the MediaPlayer 's totalDuration property. There is no
reason that you cannot seek all the way to the totalDuration value. We arbitrarily decided to seek to one second prior
to the end to let the user hear the very end of the song rather than just silence.
The assignment of the buttons' icons can be handled easily by the application's style sheet because these icons
are static. The buttons also share some of the styling of the play/pause button as shown here.
#seekStartButton {
-fx-graphic: url("resources/prev.png");
}
#seekEndButton {
-fx-graphic: url("resources/next.png");
}
#playPauseButton, #seekEndButton, #seekStartButton {
-fx-background-color: transparent;
}
 
Search WWH ::




Custom Search