How do I play sound in an application?

John Zukowski

Starting with the 1.2 release of Java, you can use the newAudioClip() method of Applet to get an AudioClip to play in an application.

File file = new File("bark.wav");
AudioClip clip = Applet.newAudioClip(file.toURL());

In prior versions of the Java libraries, you had to rely on the non-standard sun.audio package and play them with the AudioPlayer class found in that package.

Besides using File to locate the audio file in the application, you can also use the getResource() or getSystemResource() methods of ClassLoader.