Working with embedded sounds

Using embedded sounds, instead of loading sound from an external file, is most useful for small sounds that are used as indicators within your application's user interface, such as sounds that play when buttons are clicked.

When you embed a sound file in your application, the size of the resulting SWF file increases by the size of the sound file. In other words, embedding large sound files in your application can increase the size of your SWF file to an undesirable size.

The exact method of embedding a sound file into your application's SWF file varies according to your development environment.

Using an embedded sound file in Flash

The Flash authoring tool lets you import sounds in a number of sound formats and store them as symbols in the Library. You can then assign them to frames in the timeline or to the frames of a button state, use them with Behaviors, or use them directly in ActionScript code. This section describes how to use embedded sounds in ActionScript code with the Flash authoring tool. For information about the other ways to use embedded sounds in Flash, see "Importing Sounds" in Using Flash.

To embed a sound file in a Flash movie:

  1. Select File > Import > Import to Library, and then select a sound file and import it.
  2. Right-click the name of the imported file in the Library panel, and select Properties. Click the Export for ActionScript checkbox.
  3. In the Class field, enter a name to use when referring to this embedded sound in ActionScript. By default, it will use the name of the sound file in this field. If the filename includes a period, as in the name "DrumSound.mp3", you must change it to something like "DrumSound"; ActionScript does not allow a period character in a class name. The Base Class field should still show flash.media.Sound.
  4. Click OK. You might see a dialog box saying that a definition for this class could not be found in the classpath. Click OK and continue. If you entered a class name that doesn't match the name of any of the classes in your application's classpath, a new class that inherits from the flash.media.Sound class is automatically generated for you.
  5. To use the embedded sound, you reference the class name for that sound in ActionScript. For example, the following code starts by creating a new instance of the automatically generated DrumSound class:
    var drum:DrumSound = new DrumSound();
    var channel:SoundChannel = drum.play();
    

    DrumSound is a subclass of the flash.media.Sound class so it inherits the Sound class's methods and properties, including the play() method as shown above.


Flash CS3