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");
		Insets buttonContainerPadding = new Insets(1, 1, 1, 1);
		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>() {
			public void handle(ActionEvent event) {

					File file = fileChooser.showOpenDialog(stage);
					fileChooser.setTitle("Open Video File");
					String url = file.toURI().toURL().toString();
					if(player != null)
					Media media = new Media(url);
					player = new MediaPlayer(media);
				} 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");
		//this button will play the video
		play_button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {;
		//this button will pause the video
		pause_button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
		//this button will stop the video
		stop_button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(player != null)
		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.

public void start(final Stage stage) throws Exception {



Finally start the application.

public static void main(String[] 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

Notify of