0

How can I add a sound notification when a window pops up? I want to add my custom sound file, not the windows sound.

Code:

public class PopupWindow {

    Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

    public void display(String newCase) throws IOException {

        Stage window = new Stage();
        Image applicationIcon = new Image(getClass().getResourceAsStream("../images/logo.jpg"));
        window.getIcons().add(applicationIcon);
        window.initStyle(StageStyle.UNDECORATED);

        Label txtNotification = new Label("Новых заявок:");
        Label notification = new Label(newCase);
        notification.setTranslateX(120);
        notification.setStyle("-fx-text-fill: crimson");
        Button closeButton = new Button("x");
        closeButton.setTranslateX(205);
        closeButton.setTranslateY(5);
        closeButton.setOnAction(event -> window.close());

        Pane layout = new Pane();

        layout.getChildren().addAll(txtNotification, notification, closeButton);

        window.setX(primaryScreenBounds.getMinX() + primaryScreenBounds.getWidth() - 245);
        window.setY(primaryScreenBounds.getMinY() + primaryScreenBounds.getHeight() / 1.2);

        Scene scene = new Scene(layout);
        window.setScene(scene);
        layout.getStylesheets().add(this.getClass().getResource("../style/popup.css").toExternalForm());

        window.setAlwaysOnTop(true);
        window.showAndWait();
    }
}
GAntoine
  • 1,265
  • 2
  • 16
  • 28

1 Answers1

1

Play an AudioClip, just before you call showAndWait() on your pop-up window:

AudioClip plonkSound = new AudioClip("http://somehost/path/plonk.aiff");
plonkSound.play();

If you want to load the audio clip from your class path (e.g. from your jar file in a sibling location to your PopopWindow.class), then get the clip as a resource:

AudioClip plonkSound = new AudioClip(
    PopupWindow.getResource("plonk.aiff").toExternalForm()
);
jewelsea
  • 150,031
  • 14
  • 366
  • 406