Incorporating Sound in an Applet
Retrieving and using sounds in Java is similar to the support provided for images. Java supports only the Sun AU format at this time. Sound quality is better when WAV sound clips are referenced by HTML as links to external files. But in order to synchronize sound with animation in Java, it must be included in a Java applet. There are applications on the Web which will convert WAV sound files to AU file format but make sure that the result is Java compatible. The AU sound files must be saved in a directory where they can be retrieved by your applet.
The simplest way to retrieve and play a sound is through the play() method. It is included in the Applet class in the Abstract Windowing toolkit. The play method takes two arguments, the base URL such as getCodeBase() and the path for the audio clip in AU format. The applet running on this page plays Neil Armstrong's quote once and continues in an endless loop playing the jay sound clip. Notice that if you link to another page, the clip continues to play. The only way to escape the jay's call when programmed in this way is to exit the browser. In the next section we will discuss a stop method to discontinue playing the sound when the browser exits the page.
Let me mention at this point, that if you are opening a Java image or sound applet with most browsers and the path name of the image or sound file points to a local drive, Java security features will cause an exception so that the file will not be found. A Java Applet will not open a file unless it is on a network drive (or Web). The Hot Java browser available at the www.sunjava.com site has more relaxed security and will provide a more stable platform to run your Applets. You may want to download it at this point.
The code for this Java sound applet is listed below.
| import java.applet.AudioClip; public class sound extends java.applet.Applet { |
This allows us to use the Audioclip and play methods in the Applet class of the Java Abstract Windowing toolkit. We want to generate an applet . |
| public void init() { play(getCodeBase(),"quote.au"); AudioClip clip = getAudioClip(getCodeBase(),"jay.au"); clip.loop(); } } |
Play the quote directly once. The file is in the same directory as the applet. Declare clip as an instance of the class AudioClip and loop to repeat the call of the bluejay. This sound file is also in the same directory as the applet itself. |
The easiest way to copy the code for this example is to click here, copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "sound.java".
After compiling this program we need to create an HTML page with a reference to the class file. It does not need a graphic window unless you are running it with an image or animation.
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute within the window.
Starting, stopping, and starting sound again
Go to the next topic: Simple Animation