Flash 8 Documentation |
|||
| Learning ActionScript 2.0 in Flash > Creating Interaction with ActionScript > Creating interactivity and visual effects > 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.
a_thousand_ways.play_btn. stop_btn. 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.
Be careful to select the movie clip behavior. This creates a movie clip with the button on Frame 1.
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.
Be careful to select the movie clip behavior. This creates a movie clip with the button on Frame 1.
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.
For more information about the methods of the Sound class, see Sound in the ActionScript 2.0 Language Reference.
Version 8
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
Comments
Fumio Nonaka said on Jan 22, 2006 at 4:18 AM :