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.