JavaFX - Media getDuration() Method



In JavaFX, the getDuration() method is a getter method used to retrieve the duration of the media content associated with the 'Media' object without requiring any additional information.

The term 'Duration' represents the length of time or the total playing time of the media, expressed in seconds.

Note − The 'Duration' object returned may contain fractions of seconds, providing precise timing for accurate playback control and synchronization within media applications.

Syntax

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

public Duration getDuration()

Parameters

This method does not take any parameters.

Return value

This method returns the 'Duration' object, which represents the length of the media content.

Example

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

In this example, we are using an audio file and retrieving the total duration of the audio file with the help of the getDuration() method.

import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
public class MediaGetDuration1 extends Application {
   @Override
   public void start(Stage primaryStage) {
      // Create a Media object representing the audio file
      String audioFile = "./audio_video/Hero1.mp3";
      Media media = new Media(new File(audioFile).toURI().toString());

      // Creating a MediaPlayer object from the Media Object
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      mediaPlayer.setOnReady(() -> {
         // Get the duration of the media
         Duration duration = media.getDuration();
         double durationInSeconds = duration.toSeconds();
         // Print the duration
         System.out.println("Duration of the media file: " + durationInSeconds + " seconds");

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

Output

Following is the output of the code displaying the total duration of the audio file.

Duration of the media file: 149.81225 seconds

Example

In the following example, we are applying the getDuration() method for retrieving the duration of the embedded video file.

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;
import javafx.util.Duration;

public class MediaGetDuration2 extends Application {
   @Override
   public void start(Stage primaryStage) {
      String videoFile = "./audio_video/sampleTP.mp4";        
      // Create a Media object with the video file
      Media media = new Media(getClass().getResource(videoFile).toString());        

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

      // Create a MediaView and associate it with the MediaPlayer
      MediaView mediaView = new MediaView(mediaPlayer);

      // Create a StackPane to hold the MediaView
      StackPane root = new StackPane();
      root.getChildren().add(mediaView);

      Scene scene = new Scene(root, 550, 275);
      // Set the Scene to the Stage
      primaryStage.setScene(scene);
      primaryStage.setTitle("Running Media Duration Example");
      primaryStage.show();        

      // Play the media
      mediaPlayer.play();

      // Set a listener for when the MediaPlayer finishes playing
      mediaPlayer.setOnEndOfMedia(() -> {
         Duration duration = media.getDuration();
         // Convert duration from milliseconds to hours, minutes, and seconds
         long totalSeconds = (long) duration.toSeconds();
         long hours = totalSeconds / 3600;
         long minutes = (totalSeconds % 3600) / 60;
         long seconds = totalSeconds % 60;

         System.out.println("Duration of the video: " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");

         primaryStage.close();
     });
   }    
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output displaying the total duration in hours, minutes, and seconds after the completion of the video playback.

Duration of the video: 0 hours, 2 minutes, 18 seconds
Advertisements