Playing Sounds from the Library
       by 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 used ]

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.

Importing a Sound into 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:

[ explicitly 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 via code.

Making the Sound File Code Friendly
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 for ActionScript.

Once you have done that, your Class and Base class fields will become enabled and filled in for you as well:

[ 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 DiscoSound:

[ 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.

Playing the Sound using Code
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();
mySound.play();
}
playSound();

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:

import flash.media.Sound;
 
function playSound()
{
var mySound:Sound = new DiscoSound();
mySound.play();
}
playSound();

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 care of.

In the second line, when you call mySound.play(), the sound file associated with DiscoSound (in my example) begins to play!

Conclusion
That's all 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.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.