Create a video player with JavaFx

In this article you will learn how to create a video player with JavaFx. JavaFx has replaced Swing when it comes to GUI’s widget creation, therefore it is time for us to start using this toolkit from Java 8 onward if we need to create a user interface for our JAVA application.

Before we get into the programming detail, first thing we need to do here is to start a new java project in eclipse. I am sure most of you already have eclipse installed in your computer and know how to create a basic java project with it so I will leave this part to you. Also make sure you have Java SE Development Kit 8 installed in your computer before we start. After the installation make sure you do the check sum as well.

Now let get started…

First we will create the buttons for the video player. It is a very simple task, just a container plus a few buttons.

//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);

Next we will write the event handlers for the buttons and create the video player object.

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();
	}
});

What the program above does is to create a Media Player object which will be used to play the video, this object will be put under the MediaView object which will then be put under StackPane. The file chooser will be used to open a file which then will be used to create a Media object that will be used in the new Media Player. Various button handlers will be used to pause, start and stop the video player. We will create a new Media Player each time the open button has been pressed.

/*
* set up all the user interface and layout of the 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 );
		
}

All the previous program will be put under a single function for the sake of easy to maintain in the future. The above is the complete code inside that function. The full program above is the engine for the entire video player.

The next thing we need to do is just to call the user interface set up function and start the stage.

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

	uiSetup(stage);
	stage.show();

}

Below is the entire program.

package com.gamingdirectional;

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class VideoMega extends Application {

	private int CANVAS_HEIGHT = 600;
	private int CANVAS_WIDTH = 600;
	MediaPlayer player;


	/*
	* set up all the user interface and layout of the 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 );
		
	}
	
	@Override
	public void start(final Stage stage) throws Exception {

		uiSetup(stage);
		stage.show();

	}

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

}

Now start the javafx video player application.

I have lots of goodies in programming to share with you all, so kindly support me by sharing this post on any social media platform which you prefer.

Like this post? Kindly share!