2016-03-22 81 views
0

如何在彈出窗口時添加聲音通知?我想添加我的自定義聲音文件,而不是Windows聲音。如何在javafx的彈出窗口中添加聲音

代碼:

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(); 
    } 
} 

回答

1

播放的AudioClip,您在彈出窗口中調用showAndWait()之前:

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

如果你想從你的類路徑中加載音頻剪輯(例如從兄弟位置的jar文件到你的PopopWindow.class),然後獲取剪輯作爲資源:

AudioClip plonkSound = new AudioClip(
    PopupWindow.getResource("plonk.aiff").toExternalForm() 
); 
+0

感謝您的回覆,但我想添加聲音文件到我的項目,而不是網址 –

相關問題