Create a video player with JavaFX libraries

In this java example we will create a video player with the help of JavaFX libraries. The video player will consist of 4 buttons, open file, stop, pause and play. We will use eclipse ide to develop our application.

First of all lets create a class which extends the Application superclass.

public class VideoMega extends Application {
}

Within that class defined 3 variables.

private int CANVAS_HEIGHT = 600; // height of the player
private int CANVAS_WIDTH = 600;  // width of the player
private MediaPlayer player; // the video player which will be used to play video

Next is the meat part of this player, we will do all the video ui’s setup includes those buttons in one single function.

    /*
	* set up all the user interface and layout elements of that video player
	*/
	private void uiSetup(final Stage stage) {
		
		StackPane root = new StackPane();
		Scene theScene = new Scene( root, CANVAS_WIDTH, CANVAS_HEIGHT);
		
		//create all buttons
		final HBox buttonContainer = new HBox(0);
		final Button open_button = new Button("open");
		final Button pause_button = new Button("pause");
		final Button play_button = new Button("play");
		final Button stop_button = new Button("stop");
		buttonContainer.setAlignment(Pos.BOTTOM_LEFT);
		Insets buttonContainerPadding = new Insets(1, 1, 1, 1);
		buttonContainer.setPadding(buttonContainerPadding);
		buttonContainer.getChildren().addAll(open_button, play_button, pause_button, stop_button);
		
		final FileChooser fileChooser = new FileChooser(); // create a file chooser
		final MediaView mediaView = new MediaView(player); // create a media view
		
		//this button will load the video
		open_button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {

				try{
					File file = fileChooser.showOpenDialog(stage);
					fileChooser.setTitle("Open Video File");
					String url = file.toURI().toURL().toString();
					
					if(player != null)
						player.stop();
					
					Media media = new Media(url);
					
					player = new MediaPlayer(media);
					
					mediaView.setFitWidth(stage.getWidth());
					mediaView.setFitHeight(stage.getHeight());
					mediaView.setMediaPlayer(player);	
				} catch(Exception e) {
					Alert alert = new Alert(AlertType.INFORMATION);
					alert.setTitle("Loading Error");
					alert.setHeaderText("Error happen when loading");
					alert.setContentText("Cannot load the video");
					alert.showAndWait().ifPresent(null
					);
				}
			}
		});
		
		//this button will play the video
		play_button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				player.play();
			}
		});
		
		//this button will pause the video
		pause_button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				player.pause();
			}
		});
		
		//this button will stop the video
		stop_button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if(player != null)
					player.stop();
			}
		});
		
		root.getChildren().addAll(mediaView, buttonContainer); //add the media view and button container to the stackpane
		
		stage.setTitle( "Video" );
		
		stage.setScene( theScene );
		
	}

Next within the override start method called the above function and passing in the stage object.

@Override
public void start(final Stage stage) throws Exception {

	uiSetup(stage);
	stage.show();

}

Finally start the application.

public static void main(String[] args) {
        Application.launch(args);
}
JavaFx video player
JavaFx video player

The above JavaFx video player is just a framework, there are lots of components still need to be added, for example the slider and the time text, it is up to you to make it perfect!

Leave a Reply

avatar
  Subscribe  
Notify of