Loading Sounds

Much of what has been said for loading images also applies to loading sounds. There are just a few differences, however:
Currently, sound support is tied to applets only. That means that stand-alone programs can not read or play a sound clip (unless they use an additional package available for free from SUN). Images can be used in applets and stand-alone programs.
Sounds can not be added to the current version of the MediaTracker. SUN will hopefully remedy that situation soon. The getAudioClip method, as the getImage method, will return immediately.  It is somewhat unclear to me what exactly happens, when the sound starts loading, whether the sound can be played 'incrementally', or whether a 'play' method will block until the entire sound clip is downloaded, then start playing. Sound support seems to be not as well developed (and documented) as image support so far, sorry.
The only sound format currently supported is 'au' format from SUN. That means that WAV or Mac sounds need to be converted to SUN's 'au' format before they can be used by an applet. I have found that many sound converters promise to create true SUN au files, but often they were not acceptable to an applet. The best sound converter is a Unix program called 'sox'. It is fast, has no graphics interface (hence is fast) and converts pretty much anything into everything.
Here is some sample code for loading a sound. To get a sample sound, click here with the right mouse button and save to disk.
import java.applet.*;
import java.awt.*;

public class SoundTest extends Applet
{
   private AudioClip snd;
   private Button play = new Button("Play");
   private Button stop = new Button("Stop");

   public void init()
   {
      Panel buttons = new Panel();
      buttons.setLayout(new FlowLayout());
      buttons.add(play);
      buttons.add(stop);

      setLayout(new BorderLayout());
      add("North", buttons);

      loadSound();
   }
   public boolean action(Event e, Object o)
   {
      if (e.target == play)
         snd.play();
      else if (e.target == stop)
         snd.stop();
      return true;
   }
   public void loadSound()
   {
      try
      {
         snd = getAudioClip(getDocumentBase(), "sound1.au");
      }
      catch(Exception e)
      {
         System.err.println("Error loading sound");
      }
   }
}


(bgw)