Using Audio with ActionScript Load a Sound from the Library with ActionScript 3.0 Create or open a Flash document ActionScript 3.0.. Click the Window menu, click Library to open the L
Trang 1Sound is a great motivator For example, a particular piece of music can make you happy, or it can make you sad In addition, sounds can pull out childhood memories and stir emotions Sound is a powerful tool, however, different people react differently to sounds, therefore it's important that you think carefully about the sounds you add to your movies It's equally important to understand how you can control your movies using using ActionScript 3.0 code or ActionScript Behaviors in ActionScript 2.0
Using Audio with
ActionScript
Load a Sound from the Library
with ActionScript 3.0
Create or open a Flash document
(ActionScript 3.0)
Click the Window menu, click
Library to open the Library panel,
and then select an audio file from
the available Library items
Right-click (Win) or
Command-click (Mac) the audio sound, and
then click Properties Click
Advanced, if necessary.
Enter a distinctive name for the
Identifier field or use the default
Select the Export For ActionScript
check box
Leave the other fields at their
default values, and then click OK.
Click Frame 1 in a layer
Click the Window menu, and then
click Actions.
Type the ActionScript code shown
in the illustration:
Click the Control menu, point to
Test Movie, and then click Test.
10
9
8
7
6
5
4
3
2
1
9 8
3
5
Trang 2Load a Sound from the Library
with an ActionScript Behavior
Create or open a Flash document
(ActionScript 2.0)
Click the Window menu, click
Library to open the Library panel,
and then select an audio file from
the available Library items
Click the Library Options button,
and then click Properties Click
Advanced, if necessary.
Select the Export For ActionScript
check box
Enter a distinctive name for the
Identifier field or use the default
Leave the other fields at their
default values, and then click OK.
Click the Window menu, and then
click Behaviors to open the
Behaviors panel
Select a button object on the
Stage or Timeline keyframe
Click the plus (+) sign, located in
the upper-left portion of the
Behaviors panel, point to Sound,
and then click Load Sound From
Library.
Enter the name of the audio file in
the Linkage ID field
Enter a unique name in the
instance field
Select the Play This Sound When
Loaded check box
Click OK.
Select an Event to trigger the
sound If the audio file was added
to a Timeline frame, the event field
will be disabled
Click the Control menu, point to
Test Movie, and then click Test.
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
10
11
12
9
13
2 3
8 14
Trang 3You can load an MP3 music audio file using a built-in Flash behavior
The advantage to this process is that the file is loaded when needed, and it never increases the size of the original Flash movie Flash per-forms a calculation on audio files as they are loading When it has enough of an audio file, it begins playing, while it continues to down-load the remaining information in the background For large audio files, this cuts down on long wait times and keeps the visitor from becoming bored Streaming MP3 files are not part of a pre-existing Flash movie, they're just available from a common location
Loading a Streaming
MP3 File
Load a Streaming MP 3 File with
ActionScript 3.0
Create or open a Flash document
(ActionScript 3.0), and then select
a button object on the Stage
Open the Properties panel.
Enter a distinctive name for the
object in the Instance field, such
as myButton
Click Frame 1 in the actions layer
Click the Window menu, and then
click Actions.
Enter the script as shown in the
illustration:
Click the Control menu, point to
Test Movie, and then click Test.
7
6
5
4
3
2
1
1
4
6
5
See Also
See Project 2, “Loading and Formatting
Text” in the Workshops, available at
www.perspection.com, for
Action-Script 3.0 code to load external text
Trang 4Load a Streaming MP 3 File with
an ActionScript Behavior
Create or open a Flash document
(ActionScript 2.0), and then select
a button object on the Stage or
Timeline keyframe
Click the plus (+) sign, located in
the upper-left portion of the
Behaviors panel, point to Sound,
and then click Load Streaming
MP3 File.
Enter the URL to the source MP3
file
Enter a unique name in the
identifier field
Click OK.
If you selected a button object,
select an Event to trigger the
sound
Click the Control menu, point to
Test Movie, and then click Test.
7
6
5
4
3
2
1
2
3
1
5 4
Did You Know?
You can stop all sounds using an
ActionScript Behavior Create or open
a Flash document (ActionScript 2.0)
that contains playing audio files, place
a button object on the Stage, and then
select it Click the plus (+) sign in the
Behaviors panel, point to Sound, and
then click Stop All Sounds Click OK
Select an Event to stop the sound
Trang 5When you sync a sound to the Timeline, you're essentially instructing Flash how to play the sound Syncing sounds is a fundamental operation because choosing the wrong sync operation can drastically change how the sound plays out during the execution of the Flash movie Flash gives you the ability to choose a separate sync operation for each indi-vidual sound file When you place the sound on the Timeline, the Properties panel displays the audio properties for the selected sound and lets you define individual properties for every sound in your Flash document For example, you have a background music sound that's located in several scenes, and you want to make sure it doesn't play on top of itself (Sync: Stop) Or, you have a narration that you want per-fectly synced to an animation on the Timeline (Sync: Stream)
Syncing Sounds to
the Timeline
Sync Sounds to the Timeline
Select the keyframe on the
Timeline containing the sound you
want to sync
Click the Window menu, and then
click Properties to open the
Property Inspector
Click the Sync list arrow, and then
select from the following options:
◆ Event When you select Event
(default) the sound plays when
the record head reaches the
keyframe containing the sound,
and continues to play until the
end of the sound If the record
head reaches another keyframe
that contains the same sound, it
will begin playing on top of the
original sound
◆ Start Doesn’t allow the sound
to play on top of itself
◆ Stop Stops a sound if it is
already playing, without
affecting any other sounds
◆ Stream The Stream Sync
creates sounds synchronized to
the Timeline This is useful for
matching sounds to a particular
visual event in the movie If the
video can not keep up with the
audio, Flash will automatically
drop video frames to keep the
audio synchronized
3
2
1
1
2
3
Trang 6Once a sound is imported into Flash and placed on the Timeline, you can add sound effects and determine the number of loops When you loop a sound, you're instructing the Flash movie to repeat the sound a given number or times, or to loop the sound forever Some sounds loop better than others For example, you create some background music, and you want it to continue to play for as long as the visitor is on that particular page, but you don't want the sound to have a definable beginning or end In addition to loops, you can also add effects to a sound, including fades in and out, and fade to the left or right channel
The effects applied will only modify the selected audio file Each copy
of an audio file dragged into a Flash movie is controlled independently
Adding Effects and
Looping Sounds
Add Effects and Loop Sounds
Select the keyframe on the
Timeline containing the sound you
want to change
Click the Window menu, and then
click Properties to open the
Property Inspector
To add effects to the sound, click
the Effect list arrow, and then
select an option:
◆ Left Channel/Right Channel
Plays sound in the left or right
channel
◆ Fade Left To Right/Fade Right
To Left Changes sounds from
one channel to the other
◆ Fade In Increases volume
◆ Fade Out Decreases volume
◆ Custom Creates custom in and
out sound points using the Edit
Envelope
To loop a sound, click the list
arrow, and then select an option:
◆ Loop Click Loop to force the
sound into an infinite loop
◆ Repeat Click Repeat, and then
enter the number of times you
want the sound to loop (up to
65,535)
4
3
2
1
1
2
3
4