JavaFX - MediaPlayer 的 getBalance() 方法



在 JavaFX 中,MediaPlayer 類的**getBalance()** 方法是一個 getter 方法,用於獲取正在播放的媒體的音訊平衡,它控制媒體的左右輸出。

此方法返回一個介於 -1.0 到 1.0 之間的雙精度值,其中 -1.0 表示左側,1.0 表示右側,0 表示中心。

我們可以使用**setBalance()** 方法設定平衡屬性。如果我們沒有為屬性指定值,它會自動設定為 0.0。

語法

以下是 MediaPlayer 類的 'getBalance()' 方法的語法:

public final double getBalance()

引數

此方法不接受任何引數。

返回值

此方法返回一個表示媒體播放器平衡的雙精度值。

示例 1

以下是一個演示 MediaPlayer 類 **getBalance()** 方法的基本示例:

在這個例子中,我們使用媒體檔案的路徑建立一個 Media 例項。然後,我們使用 **getBalance()** 方法檢索當前平衡並將其列印到控制檯。

import javafx.application.Platform;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;
public class GetBalanceExample {
   public static void main(String[] args) {
      Platform.startup(() -> {
         File mediaPath = new File("./audio_video/Hero2.mp3");
         // Create a Media object
         Media media = new Media(mediaPath.toURI().toString());
         
         // Create a MediaPlayer object and attach the Media object
         MediaPlayer mediaPlayer = new MediaPlayer(media);
         
         // Get the current balance
         double balance = mediaPlayer.getBalance();
         
         // Output the balance value
         System.out.println("Current audio balance: " + balance);
      });
   }
}

輸出

以下是程式碼的輸出,顯示音訊的平衡設定為中心。

Current audio balance: 0.0

示例 2

在下面的示例中,我們建立一個應用程式,顯示嵌入式影片檔案的當前媒體平衡值。我們將平衡屬性設定為 1.0,並使用 **getBalance()** 方法檢索屬性的值。

import javafx.application.Application;
import javafx.application.Platform;
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 GetBalanceEx extends Application {
   @Override
   public void start(Stage primaryStage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      // Create a Media object
      Media media = new Media(mediaPath.toURI().toString());
      // Create a MediaPlayer object and attach the Media object
      MediaPlayer mediaPlayer = new MediaPlayer(media);

      // Set media balance to right
      mediaPlayer.setBalance(1.0);
      
      // creating a MediaView object from the MediaPlayer Object
      MediaView viewmedia = new MediaView(mediaPlayer);
      viewmedia.setFitHeight(270);
      viewmedia.setFitWidth(450);

      // Create a VBox to hold the label and MediaView
      VBox root = new VBox();
	  
	  // Get the current balance
      double balance = mediaPlayer.getBalance();
      // Use String.valueOf to convert double to String
      Label balanceDirection = new Label("mediaBalance: " + String.valueOf(balance));
      root.getChildren().addAll(viewmedia, balanceDirection);

      Scene scene = new Scene(root, 550, 300);

      // Set the Scene to the Stage
      primaryStage.setScene(scene);
      primaryStage.setTitle("getBalance Example");
      primaryStage.show();

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

輸出

以下是程式碼的輸出,顯示媒體平衡為 1.0,使其只能從右側聽到。

getbalance
廣告
© . All rights reserved.