Adobe Flex 3 Help

Embedded asset classes

ActionScript 3.0 uses special classes, called embedded asset classes, to represent embedded assets. An embedded asset is an asset, such as a sound, image, or font, that is included in a SWF file at compile time. Embedding an asset instead of loading it dynamically ensures that it will be available at run time, but at the cost of increased SWF file size.

var piano:PianoMusic = new PianoMusic();
var sndChannel:SoundChannel = piano.play();

Using embedded asset classes in Flex

To embed an asset in ActionScript code, use the [Embed] metadata tag. Place the asset in the main source folder or another folder that is in your project's build path. When the Adobe Flex compiler encounters an Embed metadata tag, it creates the embedded asset class for you. You can access the class through a variable of data type Class that you declare immediately following the [Embed] metadata tag. For example, the following code embeds a sound named sound1.mp3 and uses a variable named soundCls to store a reference to the embedded asset class associated with that sound. The example then creates an instance of the embedded asset class and calls the play() method on that instance:

package
{
    import flash.display.Sprite;
    import flash.media.SoundChannel;
    import mx.core.SoundAsset;

    public class SoundAssetExample extends Sprite
    {
        [Embed(source="sound1.mp3")]
        public var soundCls:Class;
        
        public function SoundAssetExample()
        {
            var mySound:SoundAsset = new soundCls() as SoundAsset;
            var sndChannel:SoundChannel = mySound.play();
        }
    }
}

To use the [Embed] metadata tag in an Adobe Flex Builder ActionScript project, you must import any necessary classes from the Flex framework. For example, to embed sounds, you must import the mx.core.SoundAsset class. To use the Flex framework, include the file framework.swc in your ActionScript build path. This will increase the size of your SWF file.

Alternatively, you can embed an asset with the @Embed() directive in an MXML tag definition. For more information, see "About embedding assets" in the Flex Developer Guide.