Playing Video

JavaFX Playing Video with JavaFX Overview, Install Java, Install Eclipse, JavaFX with Eclipse, JavaFX Architecture, JavaFX Application Structure, First JavaFX Application etc.

Playing Video

Playing Video

Playing a video in JavaFX is quite simple. We need to use the same API as we have used in the case of playing Audio files. In the case of playing video, we need to use the MediaView node to display the video onto the scene.

For this purpose, we need to instantiate the MediaView class by passing the Mediaplayer object into its constructor. Due to the fact that MediaView is a JavaFX node, we will be able to apply effects to it.

In this part of the tutorial, we will discuss the steps involved in playing video media files and some examples regarding this.

Steps to play video files in JavaFX

Instantiate the class by passing the location of the audio file in its constructor. Use the following line of code for this purpose.

  1. Media media = new Media("http://path/file_name.mp3"); 

Pass the Media class object to the new instance of object.

  1. Mediaplayer mediaPlayer = new MediaPlayer(media); 

Invoke the MediaPlayer object's play() method when onReady event is triggered.

  1. mediaPlayer.setAutoPlay(true);

Instantiate MediaView class and pass Mediaplayer object into its constructor.

  1. MediaView mediaView = new MediaView (mediaPlayer)  

Add the MediaView Node to the Group and configure Scene.

  1. Group root = new Group();  
  2. root.getChildren().add(mediaView)  
  3. Scene scene = new Scene(root,600,400);  
  4. primaryStage.setTitle("Playing Video");  




  1. package application;  
  2. import;  
  4. import javafx.application.Application;  
  5. import javafx.scene.Group;  
  6. import javafx.scene.Scene;  
  7. import;  
  8. import;  
  9. import;  
  10. import javafx.stage.Stage;  
  11. public class JavaFX_Media Example extends Application  
  12. {  
  14.     @Override  
  15.     publicvoid start(Stage primaryStage) throws Exception {  
  16.         // TODO Auto-generated method stub  
  17.         //Initialising path of the media file, replace this with your file path   
  18.         String path = "/home/hpnmaratt/Downloads/test.mp4";  
  20.         //Instantiating Media class  
  21.         Media media = new Media(new File(path).toURI().toString());  
  23.         //Instantiating MediaPlayer class   
  24.         MediaPlayer mediaPlayer = new MediaPlayer(media);  
  26.         //Instantiating MediaView class   
  27.         MediaView mediaView = new MediaView(mediaPlayer);  
  29.         //by setting this property to true, the Video will be played   
  30.         mediaPlayer.setAutoPlay(true);  
  32.         //setting group and scene   
  33.         Group root = new Group();  
  34.         root.getChildren().add(mediaView);  
  35.         Scene scene = new Scene(root,500,400);  
  36.         primaryStage.setScene(scene);  
  37.         primaryStage.setTitle("Playing video");  
  39.     }  
  40.     publicstaticvoid main(String[] args) {  
  41.         launch(args);  
  42.     }  
  43. }