JavaFX – Playing Video


JavaFX – Playing Video



”;


Video is a medium of visual communication. In our daily lives, we can observe its applications in various sectors such as the entertainment industry, news agencies, and educational platforms and many more. For example, we can see the YouTube video player in the below figure −


Sample Video

Playing Video in JavaFX

To play videos in JavaFX, we need to embed it within the application. There are different video formats available, out of which JavaFX supports only two namely MPEG-4(in short mp4) and FLV. Follow the steps given below to embed a video in JavaFX application −

  • First, instantiate Media class of the package named javafx.scene.media by passing path of the video file.

  • Next, create a MediaPlayer object by passing the Media object as a parameter value to its constructor.It will enable the media to be played in the JavaFX application.

  • Then, instantiate MediaView class by passing the MediaPlayer object to its constructor as a parameter value. Doing so will allow JavaFX application to display the video.

  • Lastly, create any layout pane and set the scene and stage with the desired dimension.

Example

The following JavaFX program demonstrates how to embed a Video into a JavaFX application. Save this code in a file with the name Javafx_Video.java.


import java.io.File;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
public class Javafx_Video extends Application {
   @Override
   public void start(Stage stage) {
      // Passing the video file to the File object
      File videofile = new File("sampleTP.mp4");
      // creating a Media object from the File Object
      Media videomedia = new Media(videofile.toURI().toString()); 
      // creating a MediaPlayer object from the Media Object
      MediaPlayer mdplayer = new MediaPlayer(videomedia);
      // creating a MediaView object from the MediaPlayer Object
      MediaView viewmedia = new MediaView(mdplayer);
      //setting the fit height and width of the media view 
      viewmedia.setFitHeight(455); 
      viewmedia.setFitWidth(500); 
      // creating video controls using the buttons
      Button pause = new Button("Pause");
      Button resume = new Button("Resume");
      // creating an HBox 
      HBox box = new HBox(20, pause, resume);
      box.setAlignment(Pos.CENTER);
      // function to handle play and pause buttons
      pause.setOnAction(act -> mdplayer.pause());
      resume.setOnAction(act -> mdplayer.play());
      // creating the root
      VBox root = new VBox(20);
      root.setAlignment(Pos.CENTER);
      root.getChildren().addAll(viewmedia, box);
      Scene scene = new Scene(root, 400, 400);
      stage.setScene(scene);
      stage.setTitle("Example of Video in JavaFX");
      stage.show();   
   }
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media Javafx_Video.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media Javafx_Video

Output

When we execute the above code, it will generate the following output.


Video Output

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *