Creating Interaction with ActionScript > Creating sound controls

Creating sound controls

To control sounds in a movie, you use the predefined Sound object. 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. The Sound object's setVolume method controls the volume and the setPan method adjusts the left and right balance of a sound.

To attach a sound to a Timeline:

1 Choose File > Import to import a sound.
2 Select the sound in the library and choose Linkage from the Options menu.
3 Select Export This Symbol and give it the identifier mySound.
4 Select frame 1 in the main Timeline and choose Window > Actions.
5 Drag the set variable action from the toolbox to the Script window.
6 Enter s in the Value box.
7 In the Toolbox list, select Objects, then select Sound, and drag new Sound to the Value box.
The code should look like this:
s = new Sound();
8 Double-click the evaluate action in the toolbox.
9 Enter s in the Expression box.
10 In the Objects category of the Toolbox list, select Sound, then drag attachSound to the Expression box.
11 Enter "mySound" in the ID argument of attachSound.
12 Double-click the evaluate action in the toolbox.
13 Enter s in the Expression box.
14 In the Objects category, select Sound, then drag start to the Expression box.
The code should look like this:
s = new Sound();
s.attachSound("mySound");
s.start();
15 Choose 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 choose Insert > Convert to Symbol. Choose the movie clip behavior.
This creates a movie clip with the button on it's first frame.
3 Select the movie clip and choose Edit > Edit Symbol.
4 Select the button and choose Window > 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 Choose Edit > Edit Movie to return to the main Timeline.
7 Select the movie clip on the Stage.
8 Enter the following actions:
onClipEvent (load) {
	top=_y;
	left=_x;
	right=_x;
	bottom=_y+100;
}

onClipEvent(enterFrame){
	if (dragging==true){
		_root.s.setVolume(100-(_y-top));
	}
}
9 Choose Control > Test Movie to use the volume slider.

To create a balance sliding control:

1 Drag a button to the Stage.
2 Select the button and choose Insert > Convert to Symbol. Choose the movie clip property.
3 Select the movie clip and choose Edit > Edit Symbol.
4 Select the button and choose Window > 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 Choose Edit > Edit Movie 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){
		_root.s.setPan((_x-center)*2);
	}
}
9 Choose Control > Test Movie to use the balance slider.

For more information about the methods of the Sound object, see their entries in the ActionScript Dictionary.