sound not playing in jar

前端 未结 5 1097
天命终不由人
天命终不由人 2020-12-22 12:37

I have packed all the class files and resources as a jar but on execution the sound files wont play. My package structure is:

+project
|_classes
|_ _*.class
         


        
5条回答
  •  情歌与酒
    2020-12-22 12:38

    This is my function for playing a looping sound file in jars, it works fine for me.

    It appears that getResourceAsStream() doesn't work with jars. however, getResource() does.

    public synchronized void alarm() {
        try {
            crit = AudioSystem.getClip();
            AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(this.getClass().getResource("critical.wav"));
            crit.open(inputStream1);
            crit.loop(Clip.LOOP_CONTINUOUSLY);
    
        } catch (Exception e) {
            System.err.println(e.getMessage());
            }
    }
    

提交回复
热议问题