JavaFX - Media 的 getWidth() 方法



在 JavaFX 中,'Media' 类的 getWidth() 方法用于获取媒体的宽度(以像素为单位)。此处,宽度表示媒体的一个属性,定义视频文件的屏幕尺寸,可以用像素来衡量。

只有当媒体完全加载并准备好播放时,此方法才能提供正确的宽度值。因此,最好在确保媒体已初始化并准备就绪后使用此方法。

注意 - 在 JavaFX 中,我们不能直接使用 Media 类获取媒体文件的宽度。但是,您可以使用 MediaPlayer 对象来实现。当媒体准备好播放时,您可以监听该事件,然后使用 getWidth() 获取宽度。

语法

'Media' 类的 'getWidth()' 方法的语法如下:

public final int getWidth()

参数

此方法不接受任何参数。

返回值

此方法返回媒体的宽度。如果宽度未定义或未知,则返回零。

示例 1

以下是 getWidth() 方法的基本示例:

在这个例子中,我们创建了一个加载视频文件的应用程序,并使用 getWidth() 方法获取视频的屏幕尺寸(以像素为单位)。

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);
   }
}

输出

以下是代码的输出,显示视频的宽度(以像素为单位)。

Width of the media content: 1280

示例 2

在这个例子中,我们开发了一个应用程序来检索视频文件的宽度并在控制台中显示它。我们使用 'onReady' 事件中的 'getWidth()' 方法来获取与播放器关联的媒体对象的宽度。

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);
   }
}

输出

以下是代码的输出:

Width of the media content: 1280
广告
© . All rights reserved.