gpt4 book ai didi

java - 向 JavaFX MediaPlayer.Status 添加更改监听器以及为什么 getStatus() 方法出现故障?

转载 作者:行者123 更新时间:2023-12-02 02:50:02 29 4
gpt4 key购买 nike

我想观察我的 MediaPlayer.Status 的值,以便将其用于控制​​目的:

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.File;

public class Main extends Application {

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

public void start(Stage primaryStage) {
Group group = new Group();
Scene scene = new Scene(group, 250, 50);

File file = new File("C:/Example.mp3");
Media media = new Media(file.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);

Button playButton = new Button("Play");
Button pauseButton = new Button("Pause");
Button stopButton = new Button("Stop");
Label infoLabel = new Label("Info");

playButton.setLayoutX(0);
playButton.setLayoutY(0);

pauseButton.setLayoutX(50);
pauseButton.setLayoutY(0);

stopButton.setLayoutX(100);
stopButton.setLayoutY(0);

infoLabel.setLayoutX(150);
infoLabel.setLayoutY(0);


ObjectProperty<MediaPlayer> info = new SimpleObjectProperty<>(mediaPlayer);

info.addListener((observable, oldValue, newValue) -> infoLabel.setText(newValue.getStatus().toString()));

playButton.addEventHandler(ActionEvent.ACTION, e -> {
mediaPlayer.play();
});

pauseButton.addEventHandler(ActionEvent.ACTION, e -> {
mediaPlayer.pause();
});

stopButton.addEventHandler(ActionEvent.ACTION, e -> {
mediaPlayer.stop();
});

group.getChildren().addAll(playButton,pauseButton,stopButton,infoLabel);
primaryStage.setScene(scene);
primaryStage.show();
}
}

不幸的是,我似乎无法获得任何更改,获得结果的唯一方法是每次单击按钮时调用 mediaPlayer.getStatus()

但即便如此,也需要单击两次才能正确更新状态。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.File;

public class Main extends Application {

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

public void start(Stage primaryStage) {
Group group = new Group();
Scene scene = new Scene(group, 250, 50);

File file = new File("C:/Example.mp3");
Media media = new Media(file.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);

Button playButton = new Button("Play");
Button pauseButton = new Button("Pause");
Button stopButton = new Button("Stop");
Label infoLabel = new Label("Info");

playButton.setLayoutX(0);
playButton.setLayoutY(0);

pauseButton.setLayoutX(50);
pauseButton.setLayoutY(0);

stopButton.setLayoutX(100);
stopButton.setLayoutY(0);

infoLabel.setLayoutX(150);
infoLabel.setLayoutY(0);

playButton.addEventHandler(ActionEvent.ACTION, e -> {
mediaPlayer.play();
infoLabel.setText(mediaPlayer.getStatus().toString());
});

pauseButton.addEventHandler(ActionEvent.ACTION, e -> {
mediaPlayer.pause();
infoLabel.setText(mediaPlayer.getStatus().toString());
});

stopButton.addEventHandler(ActionEvent.ACTION, e -> {
mediaPlayer.stop();
infoLabel.setText(mediaPlayer.getStatus().toString());
});

group.getChildren().addAll(playButton,pauseButton,stopButton,infoLabel);
primaryStage.setScene(scene);
primaryStage.show();
}
}

最佳答案

您应该能够像这样访问状态属性:

mediaPlayer.statusProperty().addListener((observable, oldValue, newValue) -> infoLabel.setText(newValue.toString()));

关于java - 向 JavaFX MediaPlayer.Status 添加更改监听器以及为什么 getStatus() 方法出现故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003174/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com