向JavaFX MediaPlayer.Status添加更改侦听器,为什么getStatus()方法会出现故障?

时间:2017-05-16 13:40:31

标签: java javafx javafx-8

我想观察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();
    }
}

1 个答案:

答案 0 :(得分:1)

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

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