JavaFX - Media getWidth() Method



In JavaFX, the getWidth() method of the 'Media' class is used to fetch the width of the media in pixels. Here, the width denotes a property of the media defining the screen size of the video files, which can be measured in pixels.

This method gives the correct width value only when the media is fully loaded and ready to play. So, it's best to use this method after ensuring the media is initialized and ready.

Note − In JavaFX, we can't directly get the width of a media file using the Media class. But you can still do it by using a MediaPlayer object. When the media is ready to play, you can listen for that event and then get the width using getWidth().

Syntax

Following is the syntax of the 'getWidth()' method of 'Media' class −

public final int getWidth()

Parameters

This method does not takes any parameters.

Return value

This method returns the width of the media. Otherwise, zero if the width is undefined or unknown.

Example 1

Following is the basic example of the getWidth() method −

In this example, we are creating an application that loads a video file and using the getWidth() method, we are obtaining the screen size of the videos in pixels.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

public class MediaGetWidth extends Application {
   @Override
   public void start(Stage primaryStage) {
      // Path to the media file
      String mediaFile = "./audio_video/sampleTP.mp4";

      // Create a Media object with the media file
      Media media = new Media(getClass().getResource(mediaFile).toString());

      // Create a MediaPlayer with the Media object
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      // Create a MediaView to display the media content
      MediaView mediaView = new MediaView(mediaPlayer);

      StackPane root = new StackPane();
      root.getChildren().add(mediaView);

      Scene scene = new Scene(root, 550, 275);
      primaryStage.setScene(scene);
      primaryStage.setTitle("Media Width Example");
      primaryStage.show();
      // Set a listener for when the MediaPlayer is ready
      mediaPlayer.setOnReady(() -> {
         // Print the width of the media content
         System.out.println("Width of the media content: " + mediaPlayer.getMedia().getWidth());
      });
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output of the code displaying the width of the video in pixels.

Width of the media content: 1280

Example 2

In this example, we are developing an application to retrieve the width of a video file and display it on the console. We use the 'getWidth()' method within the 'onReady' event to obtain the width of the media object associated with the player.

import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class MediaGetWidth1 extends Application {
   @Override
   public void start(Stage primaryStage) {
      String mediaFile = "./audio_video/sampleTP.mp4";

      // Create a Media object with the media file
      Media media = new Media(getClass().getResource(mediaFile).toString());

      // Create a MediaPlayer with the Media object
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      // Set a listener for when the MediaPlayer is ready
      mediaPlayer.setOnReady(() -> {
         // Print the width of the media content
         System.out.println("Width of the media content: " + media.getWidth());

         // Closing the application after printing the width
         primaryStage.close();
      });
      // Start the media player
      mediaPlayer.play();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output of the code −

Width of the media content: 1280
Advertisements