JavaFX Video Player with MediaView

This is a tutorial on creating a Video Player with JavaFX MediaView.

Creating a Video Player (or Media Player) in JavaFX is much simpler than it sounds. The JavaFX MediaView component takes care of almost all the work, so we only have really have to worry about the settings and the GUI window.

We’ll be using both the JavaFX MediaPlayer and the JavaFX MediaView classes here. The MediaPlayer class is what controls the video, while the MediaView class is used to actually display/play the Video/Media on screen.


Creating a Video Player in JavaFX

We’ll be going through the process of creating a Video/Media Player in JavaFX step by step, followed by a complete example at the end of the article.

Media media = new Media("https://coderslegacy.com/wp-content/uploads/2020/07/Pygame_Platformer-1.mp4");

The above line is the first and most important step to creating a media player in JavaFX. This line creates a “Media” object from a URL that points to a mp4 video.

The nice thing about this function is that it can either be a website URL or a local file path on your PC.

MediaPlayer mediaPlayer = new MediaPlayer(media); 

This line of code creates a MediaPlayer object. The MediaPlayer class in JavaFX is in charge of handling all the “controls” like playing and pausing the media etc. You must pass the media object you created earlier into it’s parameters.

MediaView mediaView = new MediaView (mediaPlayer);

The MediaView class is in-charge of actually displaying the media onto the screen. You must pass the mediaplayer object you created earlier into it’s parameters.

Play / Pause / Stop Media

MediaPlayer.play()

The play() method is used to begin the playing of the media. It will automatically unpause the media if it’s in a paused state.

MediaPlayer.pause()

Used to pause media while it’s still running. It will freeze the media as is, until it is stopped or unpaused again.

MediaPlayer.stop()

Used to cancel the media that is playing. Resets it to the beginning.


JavaFX VideoPlayer example

Below is a complete example, showcasing a complete VideoPlayer that we’ve been able to create using JavaFX.

package application;

import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;


public class Main extends Application  {
	public static void main(String args[]){          
		 launch(args);     
	} 
		
	@Override     
	public void start(Stage primaryStage) throws Exception { 
		
		String file = "C://Users/Home/Desktop/Movie.mp4";
		
		Media media = new Media(new File(file).toURI().toString());
		
		MediaPlayer mediaplayer = new MediaPlayer(media);  
		
		MediaView mediaView = new MediaView (mediaplayer);
	
		Button button1 = new Button("Play");
		Button button2 = new Button("Pause");
		Button button3 = new Button("Stop");
		
		button1.setOnAction(e -> {
			mediaplayer.play();
		});
		
		button2.setOnAction(e -> {
			mediaplayer.pause();
		});
		
		button3.setOnAction(e -> {
			mediaplayer.stop();
		});
		

		
		GridPane layout = new GridPane();
		layout.setHgap(10);
		layout.setVgap(10);
		
		//layout.add(mediaView, 0, 0);
		layout.add(mediaView, 1, 0);
		layout.add(button1, 0, 1);
		layout.add(button2, 1, 1);
		layout.add(button3, 2, 1);
		   
		Scene scene = new Scene(layout, 300, 200);	

		primaryStage.setTitle("CodersLegacy");
		primaryStage.setScene(scene);	
		primaryStage.show();
    }
}

Here are some extra methods that will come in handy when creating a more complex VideoPlayer in JavaFX.

The Media.getDuration() method will return the duration (length) of the Media content that you passed into it.

You can use the Media.getHeight() method will return the height (in pixels) of the media content you have created.

The Media.getWidth() method will return the width (in pixels) of the media content you have created.

The MediaView.setOnError() method is used for event handling, like the setOnAction method. You can link this to a function that you want to run if an error occurs.

A link to the official documentation if you’re interested in learning more.


This marks the end of the JavaFX Video Player with MediaView tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments