Java Sound |
Java Development Kittm 1.2 enables you to play many different types of audio files from both applets and applications.Playing Sounds from an Applet
The mechanism for playing sounds from an applet is unchanged in JDK 1.2. To play a sound file, you can load the clip usingApplet.getAudioClip
and control playback through theAudioClip
play
,loop
, andstop
methods.For example, to play a WAV file from an applet, you could:
- Call
Applet.getAudio
clip and pass in the URL where the.wav
file is located.- Call
play
orloop
on theAudioClip
.The audio data is loaded when the
AudioClip
is constructed--it is not loaded asynchronously.You can also use
Applet.play
to play any of the supported types of audio files. However, when you useApplet.play
, the audio data is not preloaded. The first time the user initiates playback of a particular sound, your applet's drawing and event handling will freeze while the audio data is loaded.Example: SoundApplet
This applet plays several different types of audio clips: an AU file, an AIFF file, a WAV file, and a MIDI file.
Because most browsers still don't support JDK 1.2, here's a picture of what the applet looks like:
Note: The above applet requires JDK 1.2. If you are using a browser that does not support 1.2, you won't be able to run the applet. Instead, you need to view this page in a 1.2 browser such as the JDK Applet Viewer (appletviewer
). For more information about running applets, refer to About Our Examples.You can find the complete code for this program in
Note:SoundApplet.java
. You will also need these other files: And these sound files (shift-click each link to download the file):
Regardless of the type of sound file used, the code for loading and playing the file is the same. This example provides a framework for loading and playing multiple audio clips and loads the audio clips asynchronously, but the code for loading and playing the clips essentially boils down to:
AudioClip onceClip, loopClip; onceClip = applet.getAudioClip(getCodeBase(), "file1.au"); loopClip = applet.getAudioClip(getCodeBase(), "file2.rmf"); onceClip.play(); //Play it once. loopClip.loop(); //Start the sound loop. loopClip.stop(); //Stop the sound loop.This applet stops playing a looping sound when the user leaves the page and resumes playback when the user comes back. This is done through the applet's
start
andstop
methods:public void stop() { onceClip.stop(); //Cut short the one-time sound. if (looping) { loopClip.stop(); //Stop the sound loop. } } public void start() { if (looping) { loopClip.loop(); //Restart the sound loop. } }To reduce the amount of time that the user has to wait before interacting with the applet, the sounds are preloaded in a background thread instead of in the applet's
init
method. If the user initiates playback before a sound has finished loading, the applet can respond appropriately. The actual loading of the sounds is done in theSoundLoader
run
method:public void run() { AudioClip audioClip = applet.getAudioClip(baseURL, relativeURL); soundList.putClip(audioClip, relativeURL); }
In JDK 1.2, applications as well as applets can play sounds. A new static method has been added tojava.applet.Applet
to enable applications to createAudioClips
from a URL:To play a sound from an applet, you callpublic static final AudioClip newAudioClip(URL r)Applet.newAudioClip
to load the sound and then use theAudioClip
play
,loop
, andstop
methods to control playback. For example, to play a WAV file from an application, you could:
- Call
Applet.newAudioClip
and pass in the URL where the.wav
file is located.- Call
play
orloop
on theAudioClip
.Example: SoundApplication
The sound player in the previous example can easily be implemented as an application. The main difference is thatApplet.newAudioClip
is called to load the sounds.AudioClip onceClip, loopClip;
onceClip = applet.newAudioClip(getCodeBase(), "file1.au");
loopClip = applet.newAudioClip(getCodeBase(), "file2.rmf");
Note: You can find the complete code for this program inSoundApplication.java
. You will also need these other source files: And the sound files listed previously.
Java Sound |