Wednesday, July 04, 2007

How to play a sound file in an Applet

This Java tip illustrates a method of playing audio files in Java applets. Developer needs to to take care that the audio clip resides on the same web server where your applet resides. Some time it may be the case that the developer may need to sign the applet to get the audio file from the server.
public void init() {

// The first thing is to load the audio clip
AudioClip clip = getAudioClip(getDocumentBase(),
"http://server.com/clip.au");

// Play the audio clip
clip.play();

// Stop playing audio clip
clip.stop();

// In case audio clip needs to be played continuously
clip.loop();
}

No comments: