13.2. Attaching Sounds at Runtime
Problem
You want to use ActionScript to attach a sound from the Library to your movie at runtime.
Solution
Use the Sound.attachSound( )
method.
Discussion
A sound must be attached to a movie clip before it can be played at
runtime. You can programmatically attach sounds from the Library to
Sound objects using the attachSound(
) method. To use this method properly, you must first do
three things:
Create a
Soundobject (see Recipe 13.1).Import a sound into the Library using File → Import to Library.
Set the sound symbol to export and set its linkage identifier using the Linkage option in the Library panel’s pop-up Options menu.
After successfully completing these three steps, you can call the
attachSound( ) method and pass it the linkage
identifier of the sound symbol to attach:
// IncludeSound.asto access thecreateNewSound( )method from Recipe 13.1. #include "Sound.as" // Create a newSoundobject. mySound_sound = Sound.createNewSound( ); // Attach a sound to theSoundobject. The Library sound symbol // must be set to export and have a linkage identifier that matches // the string passed toattachSound( ). mySound_sound.attachSound("MySoundSymbol");
See Also
Attached sounds do not start playing until you tell them to do so.
See Recipe 13.3 for more information
about playing sounds. Recipe 14.5
demonstrates how to use Microphone.attachAudio(
) to attach audio data from the user’s
microphone.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access