Call: (209) 560-6611

Add sound as3

Date: July 20, 2014 Author: ang Category: Tutorials Tags: , , Comments: 0

Here is a great video explaining how to add sound to your as3 scripts including how to setup a sound library.

This video shows you how to setup a sound library that references sound files externally using the URLRequest class. This is good to know, but ideally, if you're greating flash games, you want to embed your sound files. Read further down this page to learn how to embed sound files in your as3 scripts.

Embed sound files in as3

Embedding your sound files, along with all files, into your flash game is ideal. This means that your flash game will be self contained and compatible for upload to fgl.com for example. Having a self contained flash game also means that that it can more easily go viral because it's easier to share this way. Follow the instructions below to learn how to embed mp3 files in your as3 script.

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 using the Flash authoring tool:
  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 'ActionScript' tab then 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Before you post, please prove you are sentient.

What has leaves, a trunk, and branches, and grows in forests?

Before you post, please prove you are sentient.

what is 3 + 5?