play .wav file from jar as resource using java

前端 未结 9 579
离开以前
离开以前 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:26

    this worked fine for me:

    public void playSound() {
            InputStream in;
            try {
                in = new BufferedInputStream(new FileInputStream(new File(
                        getClass().getClassLoader()
                                .getResource("com/kaito/resources/sound.wav").getPath())));
                AudioStream audioStream = new AudioStream(in);
                AudioPlayer.player.start(audioStream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

提交回复
热议问题