JavaFX - MediaView setSmooth() Method



In JavaFX, the setSmooth() method in the 'MediaView' class is used to specify whether the rendering of the media content should be smoothed or not.

If the 'smoothProperty' set to 'true' then setSmooth() method uses a higher-quality filtering algorithm when scaling or transforming the video to fit within the provided bounding box. If set to 'false', it uses a faster but lower-quality filtering.

Note − The default behaviour of smooth depends on the platform configuration.

Syntax

Following is the syntax of the 'setSmooth()' method of 'MediaView' class −

public final void setSmooth(boolean value)

Parameters

This method takes one parameter −

  • value − A boolean value indicating whether smoothing should applied or not.

Return value

This method does not return any value rather it simply sets the smoothing property of the 'MediaView' instance.

Example 1

Following is a basic example demonstrating the setSmooth() method of 'MediaView' class −

In this example, we demonstrate setting the smooth property of a MediaView to 'true', which uses a higher-quality filtering algorithm when scaling or transforming the video.

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 java.io.File;
public class SetSmoothEx extends Application {
   @Override
   public void start(Stage primaryStage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());

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

      // Create a MediaView associated with the MediaPlayer
      MediaView mediaView = new MediaView(mediaPlayer);

      // Set smooth to true
      mediaView.setSmooth(true);

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

      // Set up the scene
      Scene scene = new Scene(root, 550, 270);
      primaryStage.setScene(scene);
      primaryStage.setTitle("MediaView setSmooth() Example");
      primaryStage.show();

      mediaPlayer.play();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output of the code −

setSmooth1

Example 2

In this example, we demonstrate setting the smooth property of a MediaView to 'false'. This require usage of a faster but lower-quality filtering algorithm when scaling or transforming the video. This may result in a reduction in visual quality, particularly when the video is resized or transformed.

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 java.io.File;
public class SetSmoothEx extends Application {
   @Override
   public void start(Stage primaryStage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());

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

      // Create a MediaView associated with the MediaPlayer
      MediaView mediaView = new MediaView(mediaPlayer);

      // Set smooth to true
      mediaView.setSmooth(false);

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

      // Set up the scene
      Scene scene = new Scene(root, 550, 270);
      primaryStage.setScene(scene);
      primaryStage.setTitle("MediaView setSmooth() Example");
      primaryStage.show();

      mediaPlayer.play();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output of the code −

setSmooth1
Advertisements