Sound, Part 1: Simple Background Sound

Screenshot of Simple Sound project

Click on the image above to play the Simple Sound demo in a new window. Close the window to stop the sound.

This demonstration project shows the simplest use of the Sound object: playing an extended. looping sound as ambient or background, without change or interruption.

I. Document class

As in all our Sound object demonstrations, the sound file (in MP3 format) is stored externally. There are no resources in the Library of the main movie. The main movie is attached to a document class, defined in simpleSoundDocClass.as. Here's everything within the package{} container of that class:

  //general imports
  import flash.display.MovieClip;
  import flash.events.Event;

  //sound imports
  import flash.net.URLRequest;
  import flash.media.Sound;
	
  public class simpleSoundDocClass extends MovieClip
  {
    //variables needed for Sound object
    var soundReq:URLRequest = new URLRequest("amb_1.mp3");
    var snd:Sound = new Sound();
		
    //CONSTRUCTOR
    function simpleSoundDocClass():void
    {
      //initialize the Sound
      snd.load(soundReq);
      snd.addEventListener(Event.COMPLETE, soundGood);
    }
		
    function soundGood(event:Event):void
    {
      snd.play(0,100);
    }	
  }

In the importations, we need flash.events.Event for the callback we'll be using on the Sound object (to which we're coming). We also need flash.net.URLRequest and flash.media.Sound for loading and playing our background sound.

At the top of the class file, we declare soundReq, a variable of type URLRequest, and in the same statement, we make an instance of this object pointing to the URL amb_1.mp3. You may remember URLRequest from our work with the Loader object. This is the very same object that supplies URLs to Loaders. It also works with Sounds. The form of our URL string assumes the MP3 file is stored in the same directory as the output (.swf) file.

We also instance a new Sound object, under the name snd. Like Loader, the Sound object provides a way to bring ditital media files into a Flash movie and present them to the user.

In the Constructor, we perform the load() method of the Sound object, which tells the object to find our MP3 and cue it up for playing. In ActionScript 2, we'd proceed to invoke a play() method; but ActionScript 3 wants a bit more romance.

To play our sound, we need to set up a listener/callback. The event for which we listen is called COMPLETE, and it is triggered whenever a media file finishes loading. Note that we set up this listener on the object snd, not on the main novie. Don't neglect this detail. The name of our callback method, soundGood, is of course arbitrary.

As you can see, the callback contains the play() call, which starts up the noise. The two parameters of the play method specify the amount of offset from the start of the sound (0 milliseconds, in this case), and the number of times the sound should loop (1000). These can be changed as you desire. Changing the first, offset value is like moving the needle on an old, analog record player, and dropping it into a later groove. Resetting the number of repetitions does just about what you'd expect. Set repetitions to 0 if you want to hear your sound only once.

Now you may be wondering, why do we need to go through this extended procedure? There's actually a good answer. Using a callback allows the Flash Player to wait until the sound file has indeed fully loaded before it tries to play anything. This way, you can be sure your sound will play when you need it.

II. Source files

The main movie file, simple.fla, and the document class, simpleSoundDocClass.as, can be found in the subdirectory simple, within soundDemos in the shared account on student-iat. Be sure to download the MP3 file amb_1.mp3, if you want to test this code on your own system.



University of Baltimore Logo

Last updated: 03/28/08 14:07:57
Copyright © 2008 School of Information Arts and Technologies