Java Reference
In-Depth Information
The final piece of the playback puzzle is to stop the playback once the media end.
MediaPlayer
does not stop
playback automatically when the end of the media is reached (technically, when the
stopTime
is reached; more on
that in the “Seeking” section). You can stop playback explicitly by setting the
MediaPlayer
's
onEndOfMedia
callback
property to a
Runnable
that invokes the
stop
method on the
MediaPlayer
.
songModel.getMediaPlayer().setOnEndOfMedia(() -> {
songModel.getMediaPlayer().stop();
});
Calling
MediaPlayer
's
stop
method will set the status to
STOPPED
and reset the playback position to the start of
the media by setting
currentTime
to 0.
■
it might seem a little odd that all of the
MediaPlayer
callback properties such as
onEndOfMediaProperty
,
onError
, and
onReadyProperty
are
Runnable
s rather than
EventHandler
s as is done in the rest of the JavaFX api.
the JavaFX team at Oracle has indicated that this was merely an oversight.
Note
As a final note on playback,
MediaPlayer
also contains a property named
autoPlay
. This
Boolean
property
will cause playback to start as soon as possible if it is set to true. Occasionally it is a little too aggressive and we have
found that playback will sometimes pause or even restart after a second or two when using this feature with audio
files. Therefore we recommend that the
play
method be used in normal circumstances and that
autoPlay
only
be used in situations where potential glitches in playback are not a serious concern and you just want to “fire and
forget” the playback.
Seeking
The read-only property named
currentTime
always contains the
MediaPlayer
's current playback position. This property
is of type
Duration
and can be set only by using the
seek
method. You can use the
seek
method to move the playback
position to any time between the current
startTime
and
stopTime. MediaPlayer
's
startTime
and
stopTime
properties
are initialized to 0 and
totalDuration,
respectively, where the
totalDuration
property, also read-only, specifies the
total length of the media. The
seek
method has no effect if the
MediaPlayer
is currently in the
STOPPED
state.
Three controls in the audio player application make use of the seek functionality of the
MediaPlayer
. Perhaps
the most obvious are the “seek to beginning” and “seek to end” buttons that are located on each side of the
playPauseButton
. These buttons show icons that consist of double triangles pointing to the left (seek to beginning)
and right (seek to end). Listing 9-18 shows the
createControlPanel
method from the
PlayerControlsView
class in
which these buttons are created.
Listing 9-18.
Creating the Panel Containing the Play/Pause Button and the Seek Buttons
private Node createControlPanel() {
final HBox hbox = new HBox();
hbox.setAlignment(Pos.CENTER);
hbox.setFillHeight(false);
final Button playPauseButton = createPlayPauseButton();
final Button seekStartButton = new Button();
seekStartButton.setId("seekStartButton");
Search WWH ::
Custom Search