The compilation and control of sound ActionScript

Source: Internet
Author: User
Tags range
Control preface: A good flash works, the lack of sound, just as people do not speak. and flash in the sound support is also very good, in addition to using the timeline to place sound files, we can also use as to more accurate control of the sound!

How to create a sound control in flash
If you want to control the sound in the animation, we can use the sound object in Flash to create a new sound object with the sound command. Then use the Attacesound command to connect to the sound in the library, which can be used to control the sound in the animation.
Common commands to explain:
This command is used to create a new Sound object, with the Sound object we can use as to control the sound. Sound
Attachsound ("Sounds in Library") This command enables the sound object we create to connect to the sound in the library for control.
Start ([playback from the first few seconds, cycles]) This command can be the sound start playback, which two parameters are optional.
Stop () stops the playback of the sound.
SetVolume (Volume level) This command controls the volume level of the sound, which can only be a number from 0-100.
Oh, wordy for half a day, now we should also use as the first to do a simple example.

Second, create a simple sound playback animation, you can achieve a simple start and stop playing sound.
Operation Steps:
1, create a new flash document, press Ctrl+r (file \ Import) to import a sound file, sound files can be MP3 or WAV format. After the sound file is imported, it is invisible on the stage and you must open the library to see the sound we import. As shown in figure:

2, select the sound file in the library, click the right mouse button, select "Link ..." from the menu that pops up. "Export for Action script" and "Export in first frame" option, and enter an identifier: Mysound (name can be written arbitrarily), click OK. The following figure:
 
3, back to the main stage, select the first frame of the timeline, press F9 to open the action panel. Now we're going to create a new sound object and connect to the sound in the library. Used to control sound. Enter the following script:
Music = new sound ();//Create a sound object named music.
Music.attachsound ("Mysound");//Connect this sound object to a sound with a "mysound" identifier in the library.
Complete the script as shown below:

4, now need to put two buttons on the stage to control the playback and stop the sound respectively. Select "Window"--"common library"-"button", open the public button symbol library that Flash has set for us, and then drag the play button and stop button in the circle buttons to the stage.

5, at this time, the stage will have playback and stop two buttons, select the Play button, open the Action panel, enter the following script:
On (release) {
Music.start ();
}//Play Sound
Select the Stop button and enter the following script:
On (release) {
Music.stop ();
}//Stop playing sound
6, OK, a simple control of sound playback and stop example is complete, quickly press CTRL + ENTER to test it. ( Click here to download the example source file)

Third, create a slightly more complex example, used to slide buttons to control the volume of sound.
Operation Steps:
1, create a new flash document, and press Ctrl+r to import a sound file.
2, refer to the previous example, open the library, and the imported sound file set a chain identifier. This sets the identifier to "Mysound".
3. Select the first frame of the stage timeline, open the action panel, create a new sound object, connect the sound in the library, and play the sound. The input script is as follows:
Music = new sound ();
Music.attachsound ("Mysound");
Music.start (1, 10),//playing sound, starting from the first second, and looping 10 times
4, now need a button as a sliding button, to control the volume of sound! button to make or use a button in the common library, in this case I use a button in the common library. Place a button instance at the appropriate place on the stage. The following figure:

5, select the button, open the Action panel to enter the following script:
On (Press) {
This.startdrag (False, left, top, right, bottom);/Start dragging the button, and specify its drag range, which is controlled by four variables, and the initial value of 4 variables is set later.
Drag = true;//Use a variable to determine if the button is dragged. If it is dragged, the value of the variable is true (True)
}
On (release, releaseoutside) {
This.stopdrag ()/////////////////////////// And the following drag variable is False, indicating that the button is not being dragged.
drag = false;
}
Complete script as shown in figure:

6, select this button again, and press F8 to convert it into a movie clip! The name is: Control volume. The "Control volume" movie clip contains the button just now. As shown in figure:

7, select the Stage control Volume movie clip instance, open the action panel, now to set the drag range of 4 initial values and set the volume size control. Please enter the following script:
Onclipevent (load) {//When this Flash animation begins, set the initial value of the following 4 variables. In order for the button to drag only up and down, drag the range left and right are fixed! Only let the button in the top and bottom 100 elephants drag.
top = _y;
left = _x;
right = _x;
bottom = _y+100;
}
Onclipevent (enterframe) {
if (drag = = True) {//If the button is dragged, start the following algorithm for setting the volume to control the volume.
_root.music.setvolume (100-(_y-top))//Set volume size, it is more difficult to understand the algorithm inside: 100-(_y-top), by substituting numbers to help us understand if the value of the _y is 100, Our top value is also fixed to 100. When the drag button down, the _y value will become larger, such as into 130, then _y-top equals 30, in 100-30 is equal to 70, this 70 is the level of volume. As the button drags down, the volume level becomes smaller, and the volume control is achieved.
}
}
The complete script is shown below:

8, now to test your results! ( Click here to download the example source file)

Conclusion: Through the above two examples, should have to grasp is to create sound object playback, stop sound, and control the sound of the method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.