Sound, Part 1: Simple or Background Sound
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 background, without change or interruption.
1. 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 our old friend 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. In the same statement, we make an instance of this object pointing to the URL amb_1.mp3. You should remember URLRequest from our work with the Loader object. This is the same object that supplies URLs to Loaders -- the mailing label in our package delivery analogy. It works with all sorts of applications, including XML files, and in this case, 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 uses a slightly more complicated procedure.
To play our sound, we need to set up a listener/callback pair. 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 invocation of the play() method, which initiates playback. 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; or like spinning a tape forward, if you're into slightly less antique technologies. Resetting the number of repetitions does just about what you'd expect. Set repetitions to zero (0) if you want to hear your sound only once.
You may be wondering why 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.
2. Source files
Source files for all the Sound demos presented this week are contained in a Zipped archive called soundDemos.zip, available from the code resources page.
For the present demo (simple, background sound), the main movie file, simple.fla, the document class, simpleSoundDocClass.as, and the sound source, amb_1.mp3, can be found in the subdirectory simple that is created when you unZip the archive.
|
|