Java Reference
In-Depth Information
Figure 16-5
.
Closed caption text
How It Works
The Media API has many event properties to which the developer can attach
EventHandler
s or
Runnable
s instances so they can respond when the events are
triggered. This recipe focused on the
OnMarker
event property. The
Marker
prop-
erty is responsible for receiving marker events (
MediaMarkerEvent
).
Let's begin by adding markers to the
Media
object. It contains a method
getMarkers()
that returns an
javafx.collections.ObservableMap<String, Duration>
. With an
observable map, you can add key/value pairs that represent each marker. Adding keys
should be a unique identifier, and the value is an instance of
Duration
. For simpli-
city, this example uses the closed caption text as the key for each media marker. The
marker durations are those written down as users press the pause button at points in the
video determined in Recipe 16-3. Be advised that this is not the recommended ap-
proach to use for production-quality code. You may want to use a parallel
Map
instead.
After adding markers you will be setting an
EventHandler
into the
Medi-
aPlayer
object's
OnMarker
property using the
setOnMarker()
method. Next,
you implement an
EventHandler
via a lambda expression to handle
MediaMark-
erEvent
s that are raised. Once an event has been received, you obtain the key repres-
enting the text to be used in the closed caption. The instance variable
closedCap-
tion
(
javafx.scene.text.Text
node) will simply be shown by calling the
setText()
method with the key or string associated with the marker.