Java FX 2 Alert sound

前端 未结 3 478
名媛妹妹
名媛妹妹 2021-01-02 11:05

How do I play an alert sound in Java FX 2? Is there a way of using the OS default notification sound?

3条回答
  •  醉梦人生
    2021-01-02 11:52

    This is how I play sounds,

    private void playMedia(Media m){
        if (m != null){
            MediaPlayer mp = new MediaPlayer(m);
            mp.play();
        }
    }
    
    public void playSomeSound(){
        try{
            Media someSound = new Media(getClass().getResource("some-sound.mp3").toString());
            playMedia(someSound);
        }catch(Exception ex){
            GeeLogger.error(getClass(), ex.getLocalizedMessage(), ex);
        }
    
    }
    

    Don't know about SO specifics though. Hope it helps.

提交回复
热议问题