Flash Animation 15: Starting and Stopping Sound Effects

of 09

Recap and Introduction

In Lesson 8, we covered adding a repeating audio background to animations, in very simple format; now we're going to fine-tune audio a bit more, by adding a sound effect that will only play on specific frames/events. For this, we'll need to use the Properties for our sound file, to control when it stops and starts.

I'm going to add a bouncing sound effect to the bouncing snowball animation from Lesson 14; I've already put a simple audio background on it, just a little looping holiday jingle imported into my library that helps to set the tone without being overpowering.

First you'll need to obtain an audio file for your sound effect. Short of recording your own, it's sometimes difficult to find the sound effects you need, especially with so many audio-related copyright issues at stake. I get mine from Freeze.com, which has a library of free sound effects for download. All you have to do is fill out a simple registration form, and accept or decline a couple of pages of offers.

of 09

Importing Your Sound Effect

Once you've got your sound effect, you need to import it into your Flash library. If you've forgotten how to do that, just go to File->Import to Library, and then select the file from its location on your hard drive. If your library window isn't already open, you can toggle it quickly with the F11 key to see your file in the library list.
of 09

Adding a Sound Effect Layer

Now what comes next is the tricky part. Normally when you insert audio on a Flash layer, it simply starts on load and plays continuously unless you've told it to only loop a few times; there's nothing to synch it to start and stop on a particular frame. But since we only want to play the bouncing sound effect during the frames when the actual bouncing impact is taking place, we need to use some keyframes to control our sound's stops and starts.

To do that, we'll first need to create a new layer. Mine is labeled "Sound Effects".

of 09

Inserting Your Sound Effect

While on your sound effects layer, click the Sound dropdown in the Properties bar and select the filename for your sound effect to place it on the layer.
of 09

Adding Keyframes to Control Sound

Next, we need to insert blank keyframes marking when we want our sound to stop and start. Because one frame alone is only 1/12 of a second, that's hardly enough time for a sound effect to play; in order for it to be effective, it needs to start shortly before and end shortly after the actual frame of impact. So that's where we'll insert our keyframes: one before and one after each frame where our bouncing snowball has a keyframe depicting impact with the ground. (This means we'll skip the keyframes marking the peak of the arc of ascent.) This will create a range of time for the sound effect to play in.
of 09

Adding an Audio Stop to a Keyframe

Because the bouncing snowball comes into the frame on an arc of descent, there won't be a sound at first--so I need to set the sound to stop on my first frame, so it won't start until I tell it to. To do that I've clicked on my first frame, and then in the Properties bar, selected Stop from the Sync dropdown. This sets the associated sound to stop right then, rather than play automatically. (The stop is signified by a little blue square atop the keyframe marker.)
of 09

Setting Sound to Start on a Keyframe

Now I'm going to move on to start my first sound effect. On the next keyframe, where I want my sound to start, I've once more moved to the Properties bar, and made sure my sound (boink.wav) was selected. Then under Sync I've chosen Start, which starts the sound from that keyframe; you can see the waveform for the sound appear between that keyframe and the next.
of 09

Continuing the Sound Effect to Repeat At The Right Moments

At that point it's just a matter of repeating the process on each set of keyframes; just as an extra precaution, I've put a stop on each keyframe where my sound ends, although it's really not necessary as Flash automatically terminates the sound at the next blank keyframe. When you're done, your timeline should look fairly similar to mine.
of 09

Conclusion and End Result

Now in my case, three frames wasn't long enough for my sound to play effectively; it cut off oddly and just sounded bad, so I extended out one frame to either side so that it plays for five frames. You can see my animation with timed sound effects by clicking here.

And it's as simple as that; now you can add specific sounds timed to certain instances, rather than being locked into a continuous audio track. In order to time your sounds effectively, you'll have to arrange your keys and then preview a few times before shifting things around to find the duration that works for you.