Let's start with a simple understanding of the control commands and properties of the sound that are often used in Flash:
1.new Sound ()//Create a new sound object;
2.mysound.attachsound ()//load library Sound
3.mysound.start ()//play sound;
4.mysound.getvolume ()//Read the volume of the Sound
5.mysound.setvolume ()//Set Volume
6.mysound.getpan ()//Read the balance value of sound
7.mysound.setpan ()//Set the sound balance value
Current position of 8.mysound.position//sound playback
The total length of the 9.mysound.duration//sound;
Below, we use a concrete example to learn these commands and the use of attributes:
The instance names of each component refer to Figure 1
for the convenience of learning, we combine a concrete example to practice code writing. Reader Friends can download the source file first, and I'll explain the as in this example to help you understand:
Click here to download the source file
1. First look at the action layer code:
Mysound=new Sound ()//Create a new sound object with the name Mysound;
Mysound.attachsound ("sound"); the sound in the copy library is given to the Mysound object, the target sound output designator in the library is sound, and the method of adding the output identifier
Refer to Figure 2;
Mysound.start (0,10)/start playing sound, starting from No. 0 seconds, repeat 10 times
_root.total=int (MYSOUND.DURATION/1000/60) + "min" + "" + (int (mysound.duration/1000)-int (MYSOUND.DURATION/1000/60) *60) + "seconds"//Here is the total text box to show the music of the General factory, the specific principles see note point 3;
_root.onenterframe=function () {//Every playback frame triggers the following function;
Mysound.setvolume ((a._x-30));/set volume, 30 is the leftmost coordinate of the volume line, the total length of the volume line is 100,a._x the x coordinate of the slider, minus the coordinates of the initial position 30 to get the current volume;
Mysound.setpan ((b._x-80) *2)//Set the current sound balance, relative to the balance of the middle position coordinates (80) to adjust, the range (-100,100),-100 for the left sound, 100 for the right channel, 0 for the left balance;
Txt1=mysound.getvolume ()//Read current volume and display in txt1 text box
Txt2=mysound.getpan ()//read current balance value and display in txt2 text box
}
k=0;//initializes the k variable, the function of which is described below;
_root.bb= "playing";//Let the BB text box display the word being played;
2. Control the volume on the slider code:
On (Press) {
StartDrag (a,true,30,147,130,147), or press the button to not put, start to drag slider A, drag the range left to the bottom right: 30,147,130,147. The method of obtaining these values can be seen in the property panel of the corresponding component in the source file.
}
On (release) {
Stopdrag ()//The Panasonic button stops dragging
}
3. Control the balance on the slider code:
On (Press) {
StartDrag (b,true,30,179,130,179), or press the button to not put, start to drag slider A, drag the range left to the bottom right: 30,179,130,179.
}
On (release) {
Stopdrag ()//The Panasonic button stops dragging
}
4. Pause button's code:
On (release) {
K=K+1;//K is a variable used to count the number of paused buttons currently clicked;
if (k%2==1) {//parity judgment;
Mysound.stop ();//If odd, stop music;
_root.b=int (mysound.position/1000);//record the position of playback when stopping, assign value to variable B;
_root.bb= "Stop in the first" +_root.b+ "seconds";//Display the current stop position in the BB text box;
}else{
Mysound.start (_root.b);//Continue playback from the last stop position;
_root.bb= "is playing";
}
}
5. The code on the Stop button:
On (release) {
Mysound.stop ()/Stop Music
_root.bb= "stopped";/show music stopped;
_root.b=0;//0 clicks, so that the next statistic;
_root.k=1//This explanation refer to note point 1;
}
6. Play the button code:
On (release) {
Mysound.stop ()//In order to avoid the music currently playing music and repeat the beginning of the music repetition, so first stop the current music;
Mysound.start ()//start to play new music
_root.bb= "is playing";
k=0//about why we re assigning k here, see the last note 2;
}
Attention:
1. Assign a value of K here to 1, to get the music to stop, the change of K value caused by the action of the pause button before it clears, and allows the next pause button to be executed Mysound.start (_ROOT.B), which has the function of playing the button, because the Stop command has just been executed. It doesn't make sense to execute the pause command again!
2. The k is assigned a value of 0, in order to clear the face of the pause button before the operation caused by the change of K value. Reader friend can remove this sentence and then test that you will find the click of the Play button and then click the Pause button, there will be two of voices mixed together, the emergence of this bug is precisely because of the K value in the mischief, specifically, when you do a number of operations, the value of k into odd, at the point of the play button, when the music began to play, Then you click on the Pause button, at which point the K value is even, then execute Mysound.start (_root.b), that is, then start playing a music, resulting in two sound mixed phenomenon; here, basically end the introduction of these commonly used commands, I hope to help you readers.