This TechNote explains how to create a simple sound toggle control like the one shown in the sample movie below (requires Flash Player 5 or later). The basic technique involves inserting a streaming sound inside a movie clip. The sound can be controlled with simple ActionScript 2.0 Play and Stop actions attached to buttons. Because the sound is set to Stream, the sound starts up where it was last stopped.

Before using this TechNote, be sure that you are working with a FLA file with the Script setting set to ActionScript 2.0 in the Document properties.

 

Create the sound movie clip

  1. Choose Insert > New Symbol and choose Movie Clip as the symbol's behavior. Give the movie clip a name of your choosing.
  2. Choose Import from the File menu and browse to the folder containing the sound file you want to play and import it into the movie's Library.
  3. In the first frame of the movie clip, insert add a Stop action.
  4. Select frame 2 and choose Insert > Blank Keyframe. Then drag the imported sound symbol from the Library (Window > Library) to the stage of the movie clip.
  5. Add the frame label "start" where the sound begins in frame 2.

  6. With frame 2 still selected, open the Sound panel (Window > Panels > Sound), and choose Stream from the Sync pop-up menu.
    Note: Event sounds don't work with this method.

  7. Insert frames into the movie clip's timeline until the waveform for the sound disappears, as shown below.

  8. At the frame where the waveform ends, insert a keyframe and add the following action to the frame:
    gotoAndPlay ("start");
    This action causes the playhead to return to the beginning of the sound when it's complete. If you'd prefer the sound to not loop, insert a Stop action on this frame rather than thegotoAndPlay action.

  9. Return to Edit Movie mode (Edit > Edit Movie) and drag an instance of this clip from the Library to the stage. Give the clip an instance name of "mc" in the Instance panel (Window > Panels> Instance).

Add buttons to control the sound clip

  1. Add two buttons to the stage to stop and start the sound. This example uses the gel Right and gel Stop buttons from the Buttons common Library (Window > Common Libraries > Buttons> (circle) VCR Button Set).
  2. Select the button you want to cause the sound to play and open the Actions panel (Window > Actions). Add the following ActionScript to the button:
    on (release) {
        with (mc)
        {
            play();
        }
    }

    This step causes the movie clip containing the sound to start playing, which starts the sound.

  3. Select the button you want to cause the sound to stop. Open the Actions panel (Window > Actions). Add the following ActionScript to the button:
    on (release) {
        with (mc)
        {
            stop();
        }
    }

    This button causes the movie clip containing the sound to stop playing, which stops the sound.

  4. Test the movie by choosing Control > Test Movie. If the sound doesn't start or stop on command, make sure that the instance name you've assigned to the sound clip is "mc." Also make sure that the sound's Sync property in the Sound panel is set to Stream and not Event.

Keywords: tn_14821

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy