View comments | RSS feed

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 Play 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.

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 the sound the identifier a_thousand_ways.
  4. Add a button to the Stage and name it play_btn.
  5. Add a button to the Stage and name it stop_btn.
  6. Select Frame 1 in the main Timeline, and select Window > Actions.

    Add the following code to the Actions panel:

    var song_sound:Sound = new Sound();
    song_sound.attachSound("a_thousand_ways");
    play_btn.onRelease = function() {
        song_sound.start();
    };
    stop_btn.onRelease = function() {
        song_sound.stop();
    };
    

    This code first stops the speaker movie clip. It then creates a new Sound object (song_sound) and attaches the sound whose linkage identifier is a_thousand_ways. The onRelease event handlers associated with the playButton and stopButton objects start and stop the sound by using the Sound.start() and Sound.stop() methods, and also play and stop the attached sound.

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

To create a sliding volume control:

  1. Using the Rectangle Tool, draw a small rectangle on the Stage, approximately 30 pixels high by 10 pixels wide.
  2. Select the Selection Tool and double-click the shape on the Stage.
  3. Press F8 to open the Convert to Symbol dialog box.
  4. Select the Button type, enter a symbol name of volume, and click OK.
  5. With the button symbol selected on the Stage, enter the instance name of handle_btn in the Property inspector.
  6. 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 Frame 1.

  7. Select the movie clip, and enter volume_mc as the instance name in the Property inspector.
  8. Select Frame 1 of the main Timeline, and select Window > Actions.
  9. Enter the following code into the Actions panel:
    this.createTextField("volume_txt", 10, 30, 30, 200, 20);
    volume_mc.top = volume_mc._y;
    volume_mc.bottom = volume_mc._y;
    volume_mc.left = volume_mc._x;
    volume_mc.right = volume_mc._x + 100;
    volume_mc._x += 100;
    
    volume_mc.handle_btn.onPress = function() {
        startDrag(this._parent, false, this._parent.left, this._parent.top, this._parent.right, this._parent.bottom);
    };
    volume_mc.handle_btn.onRelease = function() {
        stopDrag();
        var level:Number = Math.ceil(this._parent._x - this._parent.left);
        this._parent._parent.song_sound.setVolume(level);
        this._parent._parent.volume_txt.text = level;
    };
    volume_mc.handle_btn.onReleaseOutside = slider_mc.handle_btn.onRelease;
    

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

  10. Select Control > Test Movie to use the volume slider.

To create a sliding balance control:

  1. Use the Rectangle Tool to draw a small rectangle on the Stage, approximately 30 pixels high by 10 pixels wide.
  2. Select the Selection Tool and double-click the shape on the Stage.
  3. Press F8 to launch the Convert to Symbol dialog box.
  4. Select the Button type, enter a symbol name of balance, and click OK.
  5. With the button symbol selected on the Stage, enter an instance name of handle_btn in the Property inspector.
  6. 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 Frame 1.

  7. Select the movie clip, and enter balance_mc as the instance name in the Property inspector.
  8. Enter the following code into the Actions panel:
    balance_mc.top = balance_mc._y;
    balance_mc.bottom = balance_mc._y;
    balance_mc.left = balance_mc._x;
    balance_mc.right = balance_mc._x + 100;
    balance_mc._x += 50;
    balance_mc.handle_btn.onPress = function() {
        startDrag(this._parent, false, this._parent.left, this._parent.top, this._parent.right, this._parent.bottom);
    };
    balance_mc.handle_btn.onRelease = function() {
        stopDrag();
        var level:Number = Math.ceil((this._parent._x - this._parent.left - 50) * 2);
        this._parent._parent.song_sound.setPan(level);
    };
    balance_mc.handle_btn.onReleaseOutside = balance_mc.handle_btn.onRelease;
    

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

  9. Select Control > Test Movie to use the balance slider.

For more information about the methods of the Sound class, see Sound in the ActionScript 2.0 Language Reference.


Version 8

Comments


Fumio Nonaka said on Jan 22, 2006 at 4:18 AM :
In #6 of "To attach a sound to a timeline" the instances' names are
different from the code.

[Wrong]:
"This code first stops the speaker movie clip. It then creates a new Sound
object (song_sound) and attaches the sound whose linkage identifier is
a_thousand_ways. The onRelease event handlers associated with the
playButton and stopButton objects start and stop the sound by using the
Sound.start() and Sound.stop() methods, and also play and stop the
attached sound."

[Correct]:
This code first stops the speaker movie clip. It then creates a new Sound
object (song_sound) and attaches the sound whose linkage identifier is
a_thousand_ways. The onRelease event handlers associated with the
play_btn and stop_btn objects start and stop the sound by using the
Sound.start() and Sound.stop() methods, and also play and stop the
attached sound.
jdehaan said on Jan 23, 2006 at 1:44 PM :
Thank you! The source files have been updated and will be available in the next Flash release.
vallenwood said on Sep 13, 2006 at 1:37 AM :
In #9 of "To create a sliding volume control" there is what appears to be a typo. In the last line of the block of code, we see:

volume_mc.handle_btn.onReleaseOutside = slider_mc.handle_btn.onRelease;

Should that not be:

volume_mc.handle_btn.onReleaseOutside = volume_mc.handle_btn.onRelease;

?
greenscreensrock said on Feb 20, 2007 at 2:46 PM :
when you put this
"var song_sound:Sound = new Sound();
song_sound.attachSound("a_thousand_ways");"
on a frame, disable Control > Enable simple frame action or else, flash will crash (not responsive)

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001552.html