Java Reference
In-Depth Information
Controlling Playback
There are three main methods in the MediaPlayer class that are used to control the playback of Media objects:
play , pause , and stop . None of these methods takes any parameters, but they do affect the MediaPlayer 's status
and currentTime properties. The play method is used to begin the playback of the media. As the media are played,
MediaPlayer 's currentTime property is continuously updated to indicate the progress of the playback. Because
currentTime is of type Duration , it can give you the position of the playback in milliseconds, seconds, minutes, or hours.
The pause method will cause the playback to pause and a subsequent call to play will restart the playback from where it
was paused. Calling the stop method stops playback and resets the currentTime variable to the start of the Media .
When a MediaPlayer object is first created, its status property is initialized to MediaPlayer.Status.UNKNOWN .
Once the Media resource begins loading and there are no other errors, the status will change to MediaPlayer.Status.
READY . Calling the play method causes the status variable to change to MediaPlayer.Status.PLAYING . Calling stop
or pause will reset the status to MediaPlayer.Status.STOPPED or MediaPlayer.Status.PAUSED , respectively. Other
possible values for status include MediaPlayer.Status.HALTED, MediaPlayer.Status.STALLED and MediaPlayer.
DISPOSED . The halted state means that a critical unrecoverable error has occurred. Once entered, the MediaPlayer will
never exit the halted state. The stalled status will occur when a media stream runs out of data during playback and
must wait until more data become available. You can call the play method on the MediaPlayer at any time, even when
its status is UNKNOWN . In that case, playback will begin as soon as the MediaPlayer is in the READY state.
The MediaPlayer.DISPOSED state is entered when the dispose() method is called on the MediaPlayer . Once this
state is entered, the MediaPlayer resources are freed, and it should not be reused. However, the Media and MediaView
objects leveraged in this player can still be reused.
Laying Out the Player Controls
It is time to revisit the PlayerControlsView that we created in Listing 9-12. In addition to the openButton we currently
have, we add the ability to control playback, volume, and playback position as well as display the media's current time,
total duration, and the status of the MediaPlayer . That is a lot to display in one layout, so we use JavaFX's most flexible
layout node, the GridPane , to keep track of it all. The GridPane layout that we end up with is shown in Figure 9-5 .
Figure 9-5. The GridPane layout of the media player controls
Some controls in Figure 9-5 , such as the playback controls and the open button, span multiple rows. Others
use special alignment constraints. Listing 9-15 shows the new initView method of the PlayerControlsView that
creates this layout.
 
Search WWH ::




Custom Search