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.