Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
From the author of

Functions

Sound objects must be created by using something referred to in the Flash documentation as a constructor function.

Functions, which are also called subroutines or procedures in other computer languages, are blocks of code whose execution is deferred until called upon by ActionScript. Think of functions as a miniprograms or collections of related code that perform a particular task when (but not before) they are called upon.

In this case, we use a constructor function to create a Sound object. Sound objects are created simply by naming them in the function, which can be placed in any convenient location in the file. In my example, I included it in a frame script in frame 1.

To work with the script detailed here, you need play and stop buttons for stopping and starting the audio and a rail with a slider for controlling audio volume. Place each of these interface element on the stage into its own layer (see Figure 1).

Figure 1

Figure 1 A typical audio interface control with a rail and a slider that moves along the rail

Create a new layer labeled actions and move it to the top of the layer stack (see Figure 2).

Figure 2

Figure 2 An organized timeline with a dedicated layer for actions at the top makes it easier to find your frame scripts.

We will create our first script in frame 1 of the actions layer. The first line of the required script, reproduced below, instructs Flash to execute a function defined in the lines beneath this line when the mouse is pressed and then released over the play button:

play.onRelease = function () {

The next line is the sound constructor, which creates (or constructs) a new Sound object called mysound:

mysound = new Sound();

Once the Sound object has been created and named, you then must associate, or link to it, a sound file in the library. The next line attaches, or links, the audio file in the library with the identifier name "techno" to the new Sound object. This is not the name of the sound file in the library; it is its identifier name. (We will set up this linkage identifier shortly.)

mysound.attachSound("techno");

Now that the Sound object has been named and linked, it is ready to be controlled with ActionScript. You can use ActionScript to play, stop, pan, and control a variety of other sound properties. However, the most common application is to control sound volume.

The next line instructs Flash to start playing the sound:

mysound.start(0,2);

This start() method takes two parameters: soundOffset and Loop. The first, soundOffset, establishes the number of seconds, from its beginning, the sound will start playing. In this case, the sound starts at the beginning. The Loops parameter loops the sound playback twice.

You also need play and stop buttons and the volume slider graphics. This volume slider is the standard rail and knob. The knob moves along the rail to raise or lower the sound clip volume (refer to Figure 1).

Click the play button on the stage with the selection tool to select it. In the Instance name field of the Properties inspector, type play to add this name to this instance of the play button (see Figure 3). Naming the button instance enables you to control it with ActionScript so the audio will play when the button is clicked.

Figure 3

Figure 3 Naming the play button instance

Click the stop button on the stage with the selection tool. In the Instance name field of the Properties inspector, type stop to add this name to this instance of the stop button (see Figure 4).

Figure 4

Figure 4 Naming the stop button instance

stop.onRelease = function () {
mysound.stop();
}

Now you need to link the actual audio file in the library that will actually play to the Sound object mysound. Open the library (Ctrl+L [Windows] or Cmd+L [Mac]) and select your sound file. Click the Options button to access the drop-down menu (see Figure 5).

Figure 5

Figure 5 Options drop-down menu in the library

From the Options drop-down menu, choose Linkage to open up the Linkage dialog box (see Figure 6).

Figure 6

Figure 6 Linkage dialog box

In the Identifier field, enter the name "techno" to identify your linked sound. In the Linkage section, put checks in the Export For ActionScript and Export In First Frame check boxes. Click OK to close the Link Properties dialog box.

You can enter any unique Identifier name here, but it’s best to use a short descriptive label to help you identify this name in your scripts. The Export For ActionScript option commands Flash to export the selected sound in the SWF file using your unique identifier name so it’s available when called by the Sound object.

That’s it! Your Sound object has been created (also called instantiated) and linked to an audio file in the library.

Next, we test our script! Test your movie (Control > Test Movie) and check your script by pressing the play and stop buttons. Your audio file should play and stop accordingly.

  • + Share This
  • 🔖 Save To Your Account