Incorporating Sound in an Applet
Section 3 - Starting & Stopping Sounds.
Note: you may need to exit and restart your browser on this page to hear the desired effects.

 

In this example, the quote is allowed to finish uninterrupted as before. The clip of the bluejay that is in the continuous loop, is stopped when the page is exited but is restarted whenever the page is revisited. The stop method that is called when the browser leaves the page still contains the code to stop the bluejay sound. In addition, a start method contains code to start the jay when it is not running (== null). When an applet is running in an HTML page, the stop method is triggered when you exit that page and the start method is called when you return to the page. The start method is not quite as simple as stop. We need to set up a "Runnable" Applet by including the ' implements Runnable' option when we declare our Applet. You will see the use of 'threads' in this applet. They are not really necessary for our purposes here because the sound loops on its own. It is necessary to declare and use one in any Runnable Applet but we really don't use any of its facilities here. In the next section on animation we will cover examples where threads and multithreading are necessary to create the desired effects. For now just consider them boilerplate for our code.

 

   
 

View the Code

The code for this Java sound applet is listed below.

 

import java.applet.AudioClip;
import java.awt.Graphics;

public class sound3 extends java.applet.Applet implements Runnable
{
Thread runner;
AudioClip clip;


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 . That can be restarted (is Runnable)

We need the audio clip and a thread declared globally.
public void start() {
if (runner==null) {
runner = new Thread(this);
runner.start();
}
}
The start method checks to see if the run method is active and if not (++null) sets up a new thread and starts the running section of code.
Notice the quote sound is not started by this method.
public void stop() {
if (runner!=null){
if (clip!=null) clip.stop();
runner.stop();
runner = null;
}
}
The start method is the same as before except that we not only need to stop the sound clip but also the currently running method.

Notice the quote sound is not stopped stopped by this method. We can browse to other pages and it will still finish.
public void init()
{
play(getCodeBase(),"quote.au");
clip = getAudioClip(getCodeBase(),"jay.au");

}
Init methods are where any applet starts to execute when they are initially loaded.

We just start the quote and the bluejay sounds.
public void run()
{
clip.loop();

}

}
The run method is executed whenever a
runner.start is encountered.

This one simply restarts the looping sound clip.

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.

Although the sound quality of .au files is poor, we can maintain greater control over the sequence and duration of sounds in our web pages by using Applets. HTML pages can use better quality sound files but we can only start them and stop them when loading and exiting. There is no way to coordinate animations or any special events with sounds unless you use Java. The next section will cover animation methods and then demonstrate how to coordinate sounds within your animations.

Go to the next topic: Simple Animation

Back to the Table of Contents