Play Music simply
1. First open a new Flash file, the sound into the library (also touch the interface of friends on the Ctrl+r)
2. ID of the sound defined in the library after import, as shown in figure:
The ID here and the entity name on the scene are not the same * * *
3. Next, write the code in the first frame, as follows
Mysound = new Sound (); Defining Sound Classes
Mysound.attachsound ("tomato"); Extract the ID we set in the library
Mysound.start (); Start playing sound
4. Test results.
The beginning, stopping and cycling of music
Mysound.start ([Secondsoffset], loop);
The two parameters in start are secondsoffset, seconds is the number of seconds and offset is the meaning of cancellation or cancellation. So the simple way is to cancel the start play, in seconds to calculate ... No definition is 0, the other loop is the loop ...
Mysound.start (5, 99);
This means that the music starts playing from the 5th second and Loops 99 times, providing an example for Mysound.start (0,99);
Click to browse the file
Mysound.stop ();
Mysound.stop ("tomato"); If the new sound is not defined, use it, or multiple sounds will stop.
This is very simple don't explain it ... is to stop the music.
We see that some Web sites use a button to control playback and stop the effect is to use these can be achieved, such as:
Mysound = new Sound ();
Mysound.attachsound ("tomato");
Mysound.start (0,99); Music starts playing and loops 99 times
var music = true; Define a variable to record whether the current music is playing, because the music has been played so set to True
Btn.onrelease = function () {
if (music) {//When the variable is true, it means that it is playing
Mysound.stop (); Use stop to set music to stop
Music = false; Variable record false for music stop
} else {//below and the opposite
Mysound.start (0,99);
Music = play;
}
}
Setpan and SetVolume
Mysound.setpan (PAN);
The value of the pan is between 100 and 100, which is meant to set the balance of the horn ...-100 for the speaker on the left to hear the sound, 100 for the right, and 0 for the balance point, two speakers can hear the sound
For example:
Mysound = new Sound ();
Mysound.attachsound ("tomato");
Mysound.start (0, 10);
var speaker =-100; The variable is set to-100, which starts with the left horn.
Mysound.setpan (speaker); Set speaker balance
function pan () {//Set functions and adjust balance by setinterval per second
Speaker + 20; Balance offset 20 per second
Mysound.setpan (speaker); Set the speaker balance
if (Speaker > 100) {//Stop when music is completely offset to the right horn playback
Mysound.stop ();
Clearinterval (P);
}
}
var p = setinterval (pan, 1000); Start Horn balance per second
Mysound.setvolume (volume);
Volume is 0-100, 0 is mute, 100 is maximum
Mysound = new Sound ();
Mysound.attachsound ("tomato");
Mysound.start (0, 99);
var top = key.vol._y; Define the highest point of the drag button
var left = right = key.vol._x; Define a range to drag around
var bottom = key.vol._y+100; Define the lowest point of the drag button
key.vol.onPress = function () {
This.startdrag (True,left,top,right,bottom); Button down drag range
}
Onenterframe = function () {
v = int (key.textInput.value.text); Get the value in the input box
Mysound.setvolume (v); Set volume
}
Position, Duration and pausing
Mysound.position ();
Read-only instruction, mainly to obtain the current number of milliseconds to play music (1 seconds = 1000 milliseconds), after the music is played to be able to obtain, after the beginning of the start () after the use is not made
Mysound.duration ();
Read-only instruction, mainly to get the total number of milliseconds to music to suspend the music, playback time before continuing the pause before the start of the play, we can get the button to press the pause when the position to get the position, and then press the play again when the use of start () The Secondsoffset makes the music play from the paused part, such as:
Mysound = new Sound ();
Mysound.attachsound ("tomato");
var secondsoffset = 0; Set Secondsoffset to 0
P1.onrelease = function () {
Mysound.start (secondsoffset, 0); Play button press Start playback from 0offset
}
P2.onrelease = function () {
Secondsoffset = mysound.position/1000; Record current position and change number of seconds when the pause button is pressed
Mysound.stop (); Music paused
}
Onenterframe = function () {//Here is the loop part
if (mysound.position = = mysound.duration) {//If the number of milliseconds played equals the total number of milliseconds in music
Mysound.start (0, 99); Start Looping 99 times
}
}
As long as the above method, backward playback and fast playback is very simple, as follows:
1. Set up two buttons on the scene, respectively (Rewind rev and fast play FF)
2. Use the following code in the first frame:
Mysound = new Sound ();
Mysound.attachsound ("tomato");
Mysound.start ();
var secondsoffset = 0;
var reverse = Foward = false; Set backward and forward variable to False
Onenterframe = function () {
if (reverse && mysound.position > 0) {//When backward pressed and music seconds greater than 0
Mysound.stop (); Music stop
Secondsoffset-= 5; Offset backwards 0.5 seconds
Mysound.start (secondsoffset, 0); The music begins to play back 0.5 seconds backwards.
}
if (foward && mysound.position <= mysound.duration) {//When the fast play is pressed and the music does not end
Mysound.stop ();
Secondsoffset + + 5; Offset forward 0.5 seconds
Mysound.start (secondsoffset, 0);
}
rev.onpress = function () {//When backward press and get position
Secondsoffset = mysound.position/1000;
Reverse = true; Reverse variable is true
};
Rev.onrelease = function () {//Set reverse variable to false when backward release
Reverse = false;
}
ff.onpress = function () {//Ibid.
Secondsoffset = mysound.position/1000;
Foward = true;
};
Ff.onrelease = function () {
Foward = false;
}
};
As for the Loadsound section, write the progress bar.
1. Set up a 100%-length MC (Loadbar) on the scene
2. Use the following code in the first frame:
OnLoad = function () {
Mysound = new Sound ();
Mysound.loadsound ("Tomato.mp3"); Load the MP3 in the same directory
var percent = 0; % one starts for 0
Loadbar._xscale = percent; The width ratio of the progress bar is percent
};
Onenterframe = function () {
Mysoundbytestotal = Mysound.getbytestotal (); Gets the size of the file
mysoundbytesloaded = mysound.getbytesloaded (); To obtain the size of the current document
percent = Int (mysoundbytesloaded/mysoundbytestotal*100); Calculate the proportion of the file that is loaded
Loadbar._xscale = percent; Set progress bar width ratio
if (percent>=100) {//when fully loaded
Delete Onenterframe; Delete Loop
Mysound.start (); Music starts playing
}
};
Q1. Why can't I play SWF music in Loadmovie?
Ans: just change the Mysound = new Sound () in swf to Mysound = new Sound (this)
Q2. Why can't you set the volume of two different music at the same time? Ans: Generally you will use as
MySound1 = new Sound ();
Mysound1.attachsound ("Tomato1");
Mysound1.start ();
MySound2 = new Sound ();
Mysound2.attachsound ("Tomato2");
Mysound2.setvolume (50); Another volume of 50
Mysound2.start ();
But this is wrong, the correct way is to separate the music in different layers:
MySound1 = new Sound (this);
Mysound1.attachsound ("Tomato1");
Mysound1.start ();
Createemptymovieclip ("MC", 0);
MySound2 = new Sound (MC);
Mysound2.attachsound ("Tomato2");
Mysound2.setvolume (50);
Mysound2.start ();