Java Reference
In-Depth Information
One Player, Multiple Views
Each
MediaView
supports the concept of a viewport. This is a Rectangle2D that defines which part of the movie
to view. Multiple
MediaView
s can share the same
MediaPlayer
, and through the use of viewports, they can all be
displaying different views of the same media. Because they all share the same player, all views will be showing the
same frame of video at the same time. The viewport will become your view of the player's content. If you specify a
fitWidth
or
fitHeight
, any transforms or effects, they will be applied to the viewport just as they would have been
applied to the view as a whole. The code in Listing 9-34 shows how to define multiple
MediaView
s and viewports for a
single
MediaPlayer
.
The listing starts by defining a
Label
to hold a “secret” message, which is initially set invisible. After the
Media
object is created, two markers are defined that will tell us when it is time to split and join the two viewports that
we create. Next, two
MediaView
s are created and each is given one half of the video to view. The arguments to the
Rectangle2D
constructor specify, in order, the minimum x coordinate, the minimum y coordinate, the width, and the
height of the viewport's rectangle. Therefore, you can see that the first
MediaView
will contain a viewport into the left
side of the movie and the second
MediaView
will view the right side of the movie. A
StackPane
is created to hold the
message behind the two
MediaView
s; the message is added as the first child. When the “split” marker is triggered, the
two viewports will slide apart revealing our message. The “join” marker will trigger the two viewports to slide back
together, hiding the message once again.
Listing 9-34.
Defining Viewports for
MediaView
s
public class VideoPlayer3 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final Label message = new Label("I \u2764 Robots");
message.setVisible(false);
String workingDir = System.getProperty("user.dir");
final File f = new File(workingDir, "../media/omgrobots.flv");
final Media m = new Media(f.toURI().toString());
m.getMarkers().put("Split", Duration.millis(3000));
m.getMarkers().put("Join", Duration.millis(9000));
final MediaPlayer mp = new MediaPlayer(m);
final MediaView mv1 = new MediaView(mp);
mv1.setViewport(new Rectangle2D(0, 0, 960 / 2, 540));
StackPane.setAlignment(mv1, Pos.CENTER_LEFT);
final MediaView mv2 = new MediaView(mp);
mv2.setViewport(new Rectangle2D(960 / 2, 0, 960 / 2, 540));
StackPane.setAlignment(mv2, Pos.CENTER_RIGHT);
StackPane root = new StackPane();
root.getChildren().addAll(message, mv1, mv2);
root.setOnMouseClicked((event) -> {
mp.seek(Duration.ZERO);
Search WWH ::
Custom Search