play .wav file from jar as resource using java

前端 未结 9 589
离开以前
离开以前 2020-11-30 10:56

I want to play a .wav file using java code which is in a jar file as resource. My code is look like this -

try {
     URL defaultSound = getClass().getResou         


        
9条回答
  •  心在旅途
    2020-11-30 11:11

    Use Class.getResourceAsStream()

    Once you have a handle to the inputStream, get the audioInputStream and do the rest.

    InputStream is = getClass().getResourceAsStream("......");
    AudioInputStream ais = AudioSystem.getAudioInputStream(is);
    Clip clip = AudioSystem.getClip();
    clip.open(ais);
    

提交回复
热议问题