You use the built-in Sound object to control sounds in a movie. To use the methods of the Sound object, you must first create a new Sound object. Then you can use the attachSound method to insert a sound from the library into a movie while the movie is running.
To see an animated demonstration of sound controls, click the Play button and adjust the volume and pan.
The Sound object'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:
a_thousand_ways.playButton. stopButton. speaker.stop. Enter _root.speaker in the Object text box.new Sound. Enter song = in the Expression text box.attachSound. Enter song in the Object text box and "a_thousand_ways" (including the quotation marks) in the Parameters text box.start.play. Enter _root.speaker in the Object text box.Your code should look like this:
_root.speaker.stop();
song = new Sound();
song.attachSound("a_thousand_ways");
_root.playButton.onRelease = function() {
song.start();
_root.speaker.play();
};
onSoundComplete. Enter song in the Object text box. Enter onSoundComplete in the Method text box.stop. Enter _root.speaker in the Object text box.Your code should look like this:
_root.speaker.stop();
song = new Sound();
song.attachSound("a_thousand_ways");
_root.playButton.onRelease = function() {
song.start();
_root.speaker.play();
song.onSoundComplete = function() {
_root.speaker.stop();
};
};
To create a sliding volume control:
This creates a movie clip with the button on its first frame.
on (press) {
startDrag("", false, left, top, right, bottom);
}
on (release) {
stopDrag();
}
The startDrag parameters left, top, right, and bottom are variables set in a clip action.
onClipEvent (load) {
top = _y;
bottom = _y;
left = _x;
right = _x+100;
_x += 100;
}
onClipEvent (enterFrame) {
_root.song.setVolume(_x-left);
}
To create a sliding balance control:
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.
onClipEvent(load){
top=_y;
bottom=_y;
left=_x-50;
right=_x+50;
center=_x;
}
onClipEvent(enterFrame){
if (dragging==true){
_root.s.setPan((_x-center)*2);
}
}
For more information about the methods of the Sound object, see Sound object.