Using External Audio
Because sound files can be quite large, they are often placed in an external cast in order to keep the main movie's size smaller and facilitate quicker download times. You can't import huge sound files and expect people to patiently play break-out for 20 minutes while your movie downloads.Without compressing your sound so much as to hurt the sound quality, you can place the sound in an external cast, so that the main movie will still download quickly. This is a common technique, and will work great, as long as your audio doesn't need to loop. What's that? Can't you just check the loop option within the Property inspector? You can, but Shockwave movies won't loop sound that is in an external cast. If you turn on the Loop option, the sound will still play just one time when the movie is published as a Shockwave file.The solution is to continually check the state of the sound, and as soon as it's no longer playing to start it again. This can be accomplished using the status property of Lingo's sound object.
|1. ||Select the cast panel, and create a new, external, cast. Name the cast sound. Import bg_sound.aif and bg_sound2.aif into the new cast.To make it more interesting, we'll use two different background sounds and switch them with each new level.|
|2. ||Select the internal cast and double-click the Main Script to open it. Add the following doSound method to the script:|
By using the mod operator, the whichSound local variable will alternate between 0 and 1 no matter what the value of theLevel. This is because the mod operator returns the remainder of integer division and 2 will either go into a number evenly, leaving no remainder, or it won'tleaving 1 as a remainder.If theLevel is an odd number, then whichSound will be 1, and bg_sound will be played; otherwise bg_sound2 will play.The final line sets the volume of the sound to 180; the range is 0 to 255. Setting it to 180 makes it slightly lower than maximum, and is done because this is a background sound. Feel free to adjust the volume to your own liking.
whichSound = _global.theLevel mod 2
if whichSound = 1 then
sound(4).volume = 180
|3. ||In the Score, double-click the frame behavior at frame 25 to open it for editing. Add the following line, to call the bgSound method, to the beginSprite handler:|
Now, as soon as the playhead reaches the frame, the doBGSound method will be called, and the sound will begin playing. All you need to do now is to test the status of the sound and, if it's finished playing, call the doBGSound method.
|4. ||Within the same frame script add the following conditional test to the enterFrame handler. You can make this the very first line of code within the handler:|
When a sound channel's status property is 0, the channel is idle and no sound is playing. By testing to see if the channel is idle at each enterFrame, as soon as the sound stops playing it will be started again.Depending on your sound card and its latency, you might hear a slight pause when the sound stops, and before it starts again. However, when looping background sounds stored in external casts within Shockwave, this is about the best you can do. And depending on your card, you may not even hear a pause.
if sound(4).status = 0 then doBGSound()
|5. ||Close the script window and save the movie. When prompted, save the external cast file as sound. Publish the movie.This time when you publish the movie you may notice that Director also compacts the sound cast, creating a sound.cct file. When you place your movie on the Web, you will of course need to include this external cast file as well.That's it for this lesson, as well as the 2D shooter. In the next, and final, project you will build a 3D version of the classic game, Memory.|