Java Reference
In-Depth Information
to be of greater use to us in applets, but the former method is convenient for a sound
that needs to be played only once. This method has the following two forms:
￿ public void play(
URL <location>, String <soundFile>)
￿ public void play(URL <soundURL>)
For the fi rst version, the fi rst argument is normally the value returned by a call to
the applet's getDocumentBase method or its getCodeBase method. For example:
play(getDocumentBase(), "");
For a sound that is to be played more than once, an AudioClip reference should
be used. The address to be held in this reference is returned by method getAudio-
Clip of class Applet . This method has two forms that take the same arguments as the
above signatures for method play :
￿ public AudioClip getAudioClip(
URL <location>, String <soundFile>)
￿ public AudioClip getAudioClip(URL <soundURL>)
Once the clip has been loaded via getAudioClip , the same three methods that
were listed in the previous chapter are available for manipulating the sound fi le:
￿ void play() ;
￿ void stop() ;
￿ void loop() .
The applet below provides three buttons that will allow the user to play, stop and
loop a specifi ed sound fi le. It mirrors the SimpleSound application example from
the previous chapter and requires little commenting. To avoid having to circum-
vent the applet's security restrictions, the sound fi le is held in the same folder as
the applet.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleSoundApplet extends JApplet
implements ActionListener
private AudioClip clip;
private JButton play, stop, loop;
private JPanel buttonPanel;
public void init()
Search WWH ::

Custom Search