pek pek - 8 months ago 16
Java Question

How can I play sound in Java?

I want to be able to play sound files in my program. Where should I look?

pek pek

I personally made this code that works fine. I think it only works with .wav format.

public static synchronized void playSound(final String url) {
  new Thread(new Runnable() {
  // The wrapper thread is unnecessary, unless it blocks on the
  // Clip finishing; see comments.
    public void run() {
      try {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
          Main.class.getResourceAsStream("/path/to/sounds/" + url));;
      } catch (Exception e) {