Java Reference
In-Depth Information
final StackPane root = new StackPane();
root.getChildren().addAll(mv, markerText);
root.setOnMouseClicked((event) -> {
mp.seek(Duration.ZERO);
markerText.setText("");
});
final Scene scene = new Scene(root, 960, 540);
final URL stylesheet = getClass().getResource("media.css");
scene.getStylesheets().add(stylesheet.toString());
primaryStage.setScene(scene);
primaryStage.setTitle("Video Player 1");
primaryStage.show();
mp.play();
}
}
A
Label
is used to display the text of each marker centered at the top of the screen by setting its alignment to be
Pos.TOP_CENTER
. Then, after creating the
Media
object, four markers are inserted into the
ObservableMap
starting
with the message “Robot Finds Wall” at 3.1 seconds and ending with “And Heads for Home” at 11.5 seconds. Once
the
MediaPlayer
is instantiated, a new
MediaMarkerEvent
handler is created and passed to the
setOnMarker
method.
This event will occur on the JavaFX media thread, so we must use
Platform.runLater
to set the text of the
markerText
label on the JavaFX application thread. One other change we've made to this version of the video player is to add an
onMouseClicked
handler, which seeks back to the beginning of the media and clears the
markerText
label. This lets
you simply click on the window to restart playback. The running application displaying the “And Heads for Home”
marker text is shown in Figure
9-10
. The styling of the
messageLabel
text is included in the
media.css
style sheet that
is loaded by the
Scene
after it is created.
Figure 9-10.
Displaying markers during movie playback
Search WWH ::
Custom Search