This site requires JavaScript, please enable it in your browser!
Greenfoot back
JetLennit
JetLennit wrote ...

2013/11/12

How to play a .wav

JetLennit JetLennit

2013/11/12

#
Hi there, I was wondering how to play a .wav file in straight java using getResourceAsStream() (or something similar) any help is very appriciated! Thanks
GreenHouse GreenHouse

2013/11/12

#
public static synchronized void playSound(final String url) {
  new Thread(new Runnable() {
    public void run() {
      try {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
          Main.class.getResourceAsStream("/path/to/sounds/" + url));
        clip.open(inputStream);
        clip.start(); 
      } catch (Exception e) {
        System.err.println(e.getMessage());
      }
    }
  }).start();
}
source
JetLennit JetLennit

2013/11/12

#
Thank you so much!!
You need to login to post a reply.