Java Reference
In-Depth Information
The applet method
Applet.getAudioClip (URL url)
returns an instance of an
AudioClip
object - i.e. an object that implements the
AudioClip
interface. See the discussion in Section 6.9 on obtaining image files
for information on using the
getResources()
method in the
Class
class for
accessing audio files in a JAR file.
The following applet shows the basics of playing a sound clip. The
loop()
method continuously repeats the clip.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/** Demonstrate playing an audio clip. **/
public class
AudioTestApplet
extends JApplet
{
public void init ()
{
Container content
-
pane
=
getContentPane ();
// Create an instance of a JPanel sub-class
AudioPanel audio
-
panel
=
new AudioPanel (getClip (false));
//And add one or more panels to the JApplet panel.
content
-
pane.add (audio
-
panel);
}
// init
AudioClip getClip (boolean file
-
in
-
jar)
{
if (file
-
in
-
jar)
{
// Use getResource () to search directory or a
// jar file.
return (getAudioClip (
getClass ().getResource (
getParameter (
"
AudioClip
"
))));
}
else
{
// Read audio file from the code directory
return (getAudioClip (
getCodeBase (), getParameter
(
"
AudioClip
"
)));
}
}
// getClip
}
// class AudioTestApplet
Search WWH ::
Custom Search