JavaFX如何在播放结束后重启声音文件?

时间:2015-08-13 15:58:19

标签: audio javafx media-player

我的声音播放器有问题。我可以播放或停止声音文件但我无法在声音文件到达结束后重新启动它。每当声音文件结束时,似乎状态未知或停止,但我无法对状态作出反应。 谢谢你的帮助

public class Main extends Application {

    private StackPane root;

    @Override
    public void start(Stage primaryStage) {
        //Creating button and adding an ID for CSS
        Button btnPlay = new Button("Press to play or stop");

        Media media = new Media(this.getClass().getResource("aquarium.wav").toString());
        MediaPlayer player = new MediaPlayer(media);

        btnPlay.setOnAction(new EventHandler<ActionEvent>() {
            //handling status sound is playing or not playing
            @Override
            public void handle(ActionEvent event) {

                Status status = player.getStatus();

                if (status == Status.UNKNOWN || status == Status.HALTED) {

                    System.out.println("Status unknown");

                    //what to do??????
                }



                if (status == Status.PAUSED
                        || status == Status.READY
                        || status == Status.STOPPED) {

                    player.play();
                }
                if (status == Status.PLAYING) {

                    player.stop();

                }
            }
        });

        root = new StackPane();

        root.getChildren()
                .addAll(btnPlay);
        Scene scene = new Scene(root, 600, 450);

            //binding Scene to StyleStop.css
        scene.getStylesheets()
                .add(Main.class
                        .getResource("styleStop.css").toExternalForm());
        primaryStage.setTitle(
                "Mediaplayer - My SoundMachine");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

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

}

0 个答案:

没有答案