Hack 54 The Ubiquitous Sound-Kicker Hack

figs/moderate.gif figs/hack54.gif

Fix the Flash timeline sound-sync bug.

There is a problem in Flash with regard to the way it synchronizes sound to keyframes?you can't do it.

If you use event sounds (that is, a sound that must be fully loaded before it can be played, typically reserved for short sounds that are held in memory if needed again), they will start at the keyframe you attach them to, but the frame they end at is anyone's guess. If, for example, you start a 1-second sound on a 12-fps timeline, there is almost no chance that the sound will end exactly 12 frames later.

This "feature" causes real problems if you want to create a continuous sound consisting of a sequence of sounds attached to keyframes. If the exact frame when a sound will end is variable, you have no way of knowing when to start the next sound. For example, suppose you had two sound files, song01.wav and song02.wav, that create a continuous piece of music when played one after the other.

To mesh seamlessly, your sounds' durations must be exact multiples of the frame rate. If they are not, you can change the sound duration [Hack #57] in a sound-processing application such as Sony's Acid, Adobe Audition, or Audacity. To make sounds mesh seamlessly, you must overcome the sound-sync bug, as well as make your sounds exact multiples of the frame rate.

If you attach the two sound files to your timeline so that when one stops the other starts, as shown in Figure 7-6, you might expect there to be a seamless transition between the sounds in Flash, but there isn't, because of the aforementioned bug.

Figure 7-6. Attaching two sounds on the timeline doesn't guarantee that the second one starts when expected

Although the sound to the left finishes just before the one to the right starts, there will be a pause between the two because there is no real synchronization.

Flash guarantees synchronization only when using stream sounds (i.e., sounds that begin playing even before being downloaded in their entirety). But in some situations, stream sounds have disadvantages compared to event sounds. In particular, you have to reload a stream sound every time you want it to play, whereas event sounds stay in memory.

This hack gives you the synchronization you need by fooling the Flash Player into thinking that the timeline contains stream sounds when it actually contains event sounds.

To add a stream sound to fool Flash into synchronizing all sounds, create a keyframe in frame 5 of your timeline and attach a sound, as shown in Figure 7-7.

Figure 7-7. Attaching a stream sound to the timeline to force synchronization

Then Delete all frames in the same layer after frame 5. With the keyframe still selected, make this sound a stream sound using the Sync drop-down list in the Properties panel.

Finally, click the Edit button on the Properties panel, and use the Volume Envelope control points, as shown in Figure 7-8, to set the volume of your stream sound to zero.

Figure 7-8. Volume Envelope control for a sound

This gives you a stream sound of duration 1 frame and zero volume. This sound "kicks" the Flash Player sound engine into treating the entire timeline as if it contains stream sounds, forcing it to sync all sounds (including event sounds) to the timeline. Testing will show that your previously unsynchronized event sounds play nearly seamlessly, allowing smoother and more reliable sound transitions.

Final Thoughts

Not being able to sync event sounds has put a lot of designers off trying anything complicated with Flash sound, especially in the pre-Flash MX days before Sound.onSoundComplete( ).

Creating a SWF containing kicker-synched sounds and loading it into a level using loadMovieNum( ) is an easy way to create a downloaded sound track that contains the reusability of event sounds with the streaming ability of stream sounds. For more sound synchronization capabilities, use cue points [Hack #59] .