Creating Interaction with ActionScript > Creating complex interactivity > Creating sound controls |
![]() ![]() ![]() |
Creating sound controls
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:
1 |
Choose File > Import to import a sound. |
2 |
Select the sound in the library, right-click, and choose Options > Linkage. |
3 |
Select Export for ActionScript and Export in first frame; then give it the identifier |
4 |
Add a button to the Stage and name it |
5 |
Add a button to the Stage and name it |
6 |
Add a movie clip to the Stage and name it |
7 |
Select frame 1 in the main Timeline and choose Window > Actions. |
8 |
To pause the movie until the user selects Play, in the Actions toolbox, click the Objects category, click Movie, Sound, and Methods, and double-click |
9 |
To create a new Sound object, in the Actions toolbox, click the Objects category, click Movie, click Sound, and double-click |
10 |
In the Actions toolbox, click the Objects category, click Movie, Sound, and Methods, and double-click |
11 |
To start the song, in the Actions toolbox, click the Objects category, then click Movie, Sound, and Methods, and double-click |
12 |
To activate the speaker, in the Actions toolbox, click the Objects category, then click Movie, Movie Clip, and Methods, and double-click |
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(); }; |
|
13 |
To stop the speaker when the song ends, click the Objects category, then click Movie, Sound, and Events, and double-click |
14 |
In the Actions toolbox, click the Objects category, click Movie, Sound, and Methods, and double-click |
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(); }; }; |
|
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. Be careful to choose the movie clip behavior. |
This creates a movie clip with the button on its 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); } on (release) { stopDrag(); } |
|
The |
|
6 |
Choose 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) { _root.song.setVolume(_x-left); } |
|
9 |
Choose 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 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 |
|
6 |
Choose 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){ _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 Sound (object) in the ActionScript Dictionary.
![]() ![]() ![]() |