Playing Audio Clips

Overview

As well as support for displaying images, applets also have support for playing sound (provided that the client machine has hardware support for playing audio clips). The easiest way to play sounds in applets is by using the play() method. This is very useful for playing simple sounds, especially when starting and stopping applets. The method has the syntax:


  // In java.applet.Applet
  void play(URL url, String name)  

where the first argument is the base directory of the applet and the second argument is the sound file to play. It can be used like:


  this.play( this.getDocumentBase(), "theSound.au");

The play() method requires that the applet .class file and the sound clip are below the same directory. For a more complicated use of sounds you could use:


  // In java.applet.Applet
  AudioClip getAudioClip(URL url, String name)  

where this method returns an object of the java.applet.AudioClip Interface (no not a class, we will discuss this again). We can then use the play(), loop() or stop() methods of this object. Multiple AudioClip objects can be playing at the same time, and the resulting sound is mixed together to produce a composite.

A Sound Playing Example Applet

Task: Write an Java applet that plays a sound when it is minimized and a different sound when it is once again maximised

Solution: Ok there is no point this time to show a picture as it just plays audio, but here SoundApplet.html is my solution applet. The source code is below and at SoundApplet.java


  // The SoundApplet that plays a different sound on start and stop

  import java.applet.Applet;
  import java.applet.AudioClip;

  public class SoundApplet extends Applet
  {
	
    private AudioClip theStartSound, theStopSound;
	
    public void init()
    {
      this.theStartSound = this.getAudioClip( this.getDocumentBase(), 
                                              "start.wav" );
      this.theStopSound = this.getAudioClip( this.getDocumentBase(), 
                                              "stop.wav" );
    }
    
    public void start()
    {
      this.theStartSound.play();
    }
    
    public void stop()
    {
      this.theStopSound.play();
    }
  }

This code works perfectly, but it may not work completely as expected on your web browser. It is much easier to see working correctly if you use the appletviewer application. To make this easy I have created a .zip version that contains the source and sound files - SoundApplet.zip. Extract this file to a folder of your choice and then use the appletviewer application from the command prompt.

In this segment of code I have created two AudioClip states called theStartSound and theStopSound so as to allow these objects to be accessed from any method in the SoundApplet class. We only wish to load the sound clips from the disk once, and as we know that the init() of an applet is only called once; we should load the sound clips there. The start() and stop() methods then play the respective sounds once to completion without having to re-load them again.