Creating sound controls

You use the built-in Sound class to control sounds in a SWF file. To use the methods of the Sound class, you must first create a Sound object. Then you can use the attachSound() method to insert a sound from the library into a SWF file while the SWF file is running.

To see an animated demonstration of sound controls, click the Play button and adjust the volume and pan.

The Sound class's setVolume() method controls the volume, and the setPan() method adjusts the left and right balance of a sound.

The following procedures show how to create sound controls like the ones shown above.

To attach a sound to a Timeline:

  1. Select File > Import to import a sound.
  2. Select the sound in the library, right-click (Windows) or Control-click (Macintosh), and select Linkage.
  3. Select Export for ActionScript and Export in First Frame; then give it the identifier a_thousand_ways.
  4. Add a button to the Stage and name it playButton.
  5. Add a button to the Stage and name it stopButton.
  6. Add a movie clip to the Stage and name it speaker.
  7. Select Frame 1 in the main Timeline, and select Window > Development Panels > Actions. Add the following code to the Actions panel:
    speaker.stop();
    song = new Sound();
    song.onSoundComplete = function() {
      speaker.stop();
    };
    song.attachSound("a_thousand_ways");
    playButton.onRelease = function() {
      song.start();
      speaker.play();
    
    };
    stopButton.onRelease = function () {
      song.stop();
      speaker.stop();
    }
    

    This code first stops the speaker movie clip. It then creates a new Sound object (song) and attaches the sound whose linkage identifier is a_thousand_ways. Next, it defines an onSoundComplete handler for the song object, which stops the speaker movie clip once the sound has finished. Lastly, onRelease handlers associated with the playButton and stopButton objects start and stop the sound using the Sound.start() and Sound.stop() methods, and also play and stop the speaker movie clip.

  8. Select Control > Test Movie to hear the sound.

To create a sliding volume control:

  1. Drag a button to the Stage.
  2. Select the button and select Modify > Convert to Symbol. Be careful to select the movie clip behavior.

    This creates a movie clip with the button on its first frame.

  3. Select the movie clip and select Edit > Edit Selected.
  4. Select the button and select Window > Development Panels > Actions.
  5. Enter the following actions:
    on (press) {
        startDrag(this, false, left, top, right, bottom);
    }
    on (release) {
        stopDrag();
    }
    

    The startDrag() parameters left, top, right, and bottom are variables set in a clip action.

  6. Select Edit > Edit Document to return to the main Timeline.
  7. Select the movie clip on the Stage.
  8. Enter the following actions:
    onClipEvent (load) {
        top = _y;
        bottom = _y;
        left = _x;
        right = _x+100;
        _x += 100;
    }
    onClipEvent (enterFrame) {
        _parent.song.setVolume(_x-left);
    }
    
  9. Select Control > Test Movie to use the volume slider.

To create a sliding balance control:

  1. Drag a button to the Stage.
  2. Select the button and select Insert > Convert to Symbol. Select the movie clip property.
  3. Select the movie clip and select Edit > Edit Symbol.
  4. Select the button and select Window > Development Panels > Actions.
  5. Enter the following actions:
    on (press) {
      startDrag ("", false, left, top, right, bottom);
      dragging = true;
    }
    on (release, releaseOutside) {
      stopDrag ();
      dragging = false;
    }
    

    The startDrag() parameters left, top, right, and bottom are variables set in a clip action.

  6. Select Edit > Edit Document to return to the main Timeline.
  7. Select the movie clip on the Stage.
  8. Enter the following actions:
    onClipEvent(load){
      top=_y;
      bottom=_y;
      left=_x-50;
      right=_x+50;
      center=_x;
    }
    
    onClipEvent(enterFrame){
      if (dragging==true){
        _parent.setPan((_x-center)*2);
      }
    }
    
  9. Select Control > Test Movie to use the balance slider.

For more information about the methods of the Sound class, see Sound class.