JavaFX - MediaPlayer getStatus() Method



In JavaFX, the getStatus() of the 'MediaPlayer' class is used to retrieves the current status of the media playback. It provides information about whether the media is playing, paused, stopped, or in some other state.

Following are the statuses that can be shown by this method −

  • UNKNOWN − Indicates that the status of the media player is unknown.

  • READY − Indicates that the media player is ready to play media, but it is not playing anything at the moment.

  • PAUSED − Indicates that media playback is paused.

  • PLAYING − Indicates that media playback is currently active.

  • STOPPED − Indicates that media playback has been stopped.

Syntax

Following is the syntax of the 'getStatus()' method of 'MediaPlayer' class −

public final MediaPlayer.Status getStatus()

Parameters

This method does not take any parameters.

Return value

This method returns a 'MediaPlayer.Status' enumeration value representing the current status of the media player.

Example

Following is a basic example demonstrating the getStatus() method of the 'MediaPlayer' class −

In this example, we create a 'Media' object and a corresponding 'MediaPlayer'. We display the media status in a label within a 'VBox'. We continuously update the status label using the 'getStatus()' method.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;

public class GetStatusExample extends Application {
   @Override
   public void start(Stage primaryStage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      // creating a MediaView object from the MediaPlayer Object
      MediaView viewmedia = new MediaView(mediaPlayer);
      viewmedia.setFitHeight(250); 
      viewmedia.setFitWidth(420);

      // Create a label to display the media status
      Label statusLabel = new Label();

      // Create a VBox to hold the label
      VBox root = new VBox(20);
      root.getChildren().addAll(viewmedia, statusLabel); 

      Scene scene = new Scene(root, 550, 270);
      primaryStage.setScene(scene);
      primaryStage.setTitle("GetStatus Example");
      primaryStage.show();

      mediaPlayer.play();
      // Check the status of the media every second
      javafx.animation.Timeline timeline = new javafx.animation.Timeline(
         new javafx.animation.KeyFrame(
            javafx.util.Duration.seconds(1),
            event -> {
               // Get the status of the media
               MediaPlayer.Status status = mediaPlayer.getStatus();
               statusLabel.setText("Media Status: " + status);
            }
         )
      );
      timeline.setCycleCount(javafx.animation.Animation.INDEFINITE);
      timeline.play(); // Start the timeline
   }
   public static void main(String[] args) {
     launch(args);
   }
}

Output

Following is the output of the code displaying the media status −

getstatus
Advertisements