Applet Simple Sound Example

Let's first look at the simplest example of how we can load a sound file into a Java applet and play it. After we have shown a similar example for a Java application, we will then take a look into the Java Sound API, which gives us much more control over our sounds and access to MIDI also. Here is the code for our example applet:

Code Listing 11-1: Loading and playing a sound in an applet

start example
import javax.swing.*; import java.applet.*; import java.net.*;         public class SimpleSoundApplet extends JApplet {     public void init()     {         // Attempt to load our 'siren.wav' file...         try         {             theSound = getAudioClip(new URL(getDocumentBase(),                "siren.wav"));         }         catch(MalformedURLException e)         {             System.out.println(e);             }                  // Play the audio clip...         theSound.play();     }          private AudioClip theSound; }
end example

To execute this example, we also need to create the following HTML file in the directory in which we compile the applet in order to load it into the web browser. Here is the listing for the HTML file that we require:

Code Listing 11-2: HTML for viewing the applet

start example
<html>  <title>Simple Applet Sound</title>      <body bgcolor="#FFFFFF">       <p align="center">    <b>     Simple Sound Example    </b>   </p>       <p align="center">    <applet code="SimpleSoundApplet.class" width="100"      height="100"></applet>   </p> </html>
end example

When we view the HTML page that we have created, it will load our applet and play the wav file siren.wav, which we specified in the code (note that this file can be found on the companion CD-ROM). Note also that when the sound is played, you will recognize it as the most annoying sound ever made. When the applet loads, it will appear as a simple gray square, as we have not declared anything to appear on it; all we have done is play the sound. Here is how it looks in the web browser:

click to expand
Figure 11-1: Simple applet sound example

Let's now take a look at the code and see how it works. First we attempt to load our sound file using the following segment of code:

try {     theSound = getAudioClip(new URL(getDocumentBase(), "siren.wav")); } catch(MalformedURLException e) {     System.out.println(e);     }

As you can see, we call the getAudioClip method, which is a member of the JApplet class inherited from java.applet.Applet. The getAudioClip method takes a URL object as a parameter, which we create using the getDocumentBase method as the first parameter and then the actual filename of our audio clip as the second parameter. Note that we can use any of the three supported file types here. Note also that we need to catch a MalformedURLException, which catches an invalid URL (e.g., the file did not exist or could not be found).

Once our audio clip is loaded, we can then play it by calling its play method. This can be seen in the following line of code:

theSound.play();

Note also that the AudioClip class declares methods called stop and loop. The stop method simply stops the audio clip playback, and the loop method can be called instead of the play method to indefinitely loop the audio clip.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net