kirupa | 27 May 2010
In Flash, your
Library is a giant repository of all of the assets that
you are currently using in your project or have used at some
point inside your project:
[ the Library
is a bucket of stuff you are currently using or have
The assets you store may include, among other things,
sound files. That little detail is important because, in
this tutorial, you will learn how to play sound from a sound
file that lives inside your Library.
The first step is to get a sound file added your library.
You can do that in a variety ways. You can go to File |
Import | Import to Library:
import to the Library ]
Another way is by dragging the sound file and just
dropping it on your artboard.
Regardless of which approach you take, the end result
should be that your sound file appears in your Library:
[ your sound
file is displayed with a sound icon ]
Once your sound file is in the Library, you need to make
some tweaks to its properties so that you can reference it
Right now, while your sound file exists in the Library, it
is pretty isolated from the rest of your project. We need to
modify its properties so that it can be used. In your
Library, right-click on your sound file and, from the menu
that appears, select Properties.
The Sound Properties window will appear with some basic
settings shown. What we want to do is far from basic, so
click on the Advanced button found on the bottom-right
corner of the window:
[ click on
the Advanced button to display some more properties ]
Once you have clicked on the Advanced button, your Sound
Properties window will, believe it or not, get even taller
with more properties displayed:
[ you will
now see some Advanced properties ]
Clicking the Advanced button exposes properties that you
will need to set in order to make your sound file usable via
code. In the Linkage section, check the box marked Export
Once you have done that, your Class and Base
class fields will become enabled and filled in for you
[ a default value will be provided as the Class name ]
By default, your Class value will be the name of your
sound file. Give it a better name Ė preferably one that
didnít involve the file extension. Because my sound file has
a disco theme to it, Iím going to call my class
[ enter a smarter value for your Class name ]
Once you have given a meaningful name for the Class, go
ahead and click OK to accept these changes and to close the
Sound Properties dialog.
To play the sound file whose properties you tweaked earlier,
simply use the following code:
- import flash.media.Sound;
- function playSound()
- var mySound:Sound = new DiscoSound();
The code is actually very straightforward because all of
the logic you would normally need for loading a sound file
is not necessary because the sound file is already contained
inside your SWF file.
The two relevant lines are the following:
- var mySound:Sound = new DiscoSound();
In the first line, I am declaring my Sound object called
mySound, but notice what I
am initializing this object with. Instead of initializing
with another object with the type Sound, I am actually
initializing an object whose type is DiscoSound.
If you remember, DiscoSound was the value I gave our
sound file's Class field in the Advanced Properties earlier.
By initializing my Sound object as DiscoSound, everything
needed to associate the sound file from the Library is taken
In the second line, when you call
mySound.play(), the sound
file associated with DiscoSound (in my example) begins to
there is to playing a sound file that exists in your
Library. This approach is nice because you do not have to
worry about preloading an external sound file and playing it
once it has fully loaded. If you are curious, that approach
is described in the
Playing Sounds in AS3 tutorial. The downside of using
the Library is that it does increase the size of your SWF
file, so it is best to reserve the Library for smaller
sounds and use the external approach for larger sound clips
that can be loaded on-demand.
If you have questions, need some assistance on this topic, or just want to
chat - post in the comments below or drop by our friendly forums
(where you have a lot more formatting options) and post your question. There are
a lot of knowledgeable and witty people who would be happy to help you out
Did you enjoy reading this and found it useful? If so, please share it with
If you didn't like it, I always like to hear how I can do better next time.
Please feel free to contact me directly via e-mail, facebook, or twitter.
I like to talk a lot - A WHOLE LOT. When I'm not talking, I've been known to write the occasional English word. You can learn more about me by going here.