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());
clip.play();

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.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact