Java Reference
In-Depth Information
You'll be happy to know that you've been using event properties and implementing
Runnable s all along, albeit usually in the form of lambda epresssions. Hopefully you
noticed this in all the recipes in this chapter. When the media player is in a ready state,
the Runnable code will be invoked. Why is this correct? Well, when the media play-
er is finished loading the media, the onReady property will be notified. That way you
can be sure you can invoke the MediaPlayer 's play() method. I trust that you
will get used to event style programming. The following code snippet demonstrates set-
ting a Runnable instance into a media player object's OnReady property using a
lambda expression:
mediaPlayer.setOnReady(() -> {
mediaPlayer.play();
});
So that you can see the difference between the newer Java 8 style of programming
versus the older style, here is the same code implemented without using a lambda ex-
pression:
mediaPlayer.setOnReady(new Runnable() {
@Override
public void run() {
mediaPlayer.play();
}
});
See how many lines of code you got rid of by using lambdas? How much are you
enjoying Java 8 now? You will be taking steps similar to the onReady property. Once
a Paused event has been triggered, the run() method will be invoked to present to
the user a message containing a Text node with the word Paused and a duration
showing the time in milliseconds into the video. Once the text is displayed, you might
want to write down the duration as markers (as you'll learn in Recipe 16-4). The fol-
lowing code snippet shows an attached Runnable instance, which is responsible for
displaying a paused message and duration in milliseconds at the point at which it was
paused in the video:
// when paused event display pause message
mediaPlayer.setOnPaused(() -> {
pauseMessage.setText("Paused \nDuration: " +
Search WWH ::




Custom Search