Java Reference
In-Depth Information
Listing 9-31.
A Minimalist but Functional Movie Player
public class VideoPlayer1 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
String workingDir = System.getProperty("user.dir");
File f = new File(workingDir, "../media/omgrobots.flv");
Media m = new Media(f.toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
MediaView mv = new MediaView(mp);
StackPane root = new StackPane();
root.getChildren().add(mv);
primaryStage.setScene(new Scene(root, 960, 540));
primaryStage.setTitle("Video Player 1");
primaryStage.show();
mp.play();
}
}
This example loads the video file from the file system. Loading video from the Internet using an HTTP URL will
work just as well, but it is not advisable to package movies of any significant size inside your jar file because that will
drastically increase the size of your application. Once we have our file URL, everything becomes familiar. You use
the URL to create the
Media
object and wrap that
Media
object in a
MediaPlayer
. The only new step is to construct
a
MediaView
node using the
MediaPlayer
as the constructor argument and add it to the root of our scene graph, a
StackPane
in this case. We create a
Scene
that is sized to fit the dimensions of our movie, then pass it to the stage and
show it. The final step is to start the movie playing with the same familiar call to
MediaPlayer
's
play
method. Figure
9-9
shows this simple movie player in action.
Search WWH ::
Custom Search