Today, I suddenly discovered that I had spent a long time in Flash. Today I began to review and learn more, and today I began to learn about sounds.
1. Load the sound in the database
File -- import --- import to database
2. Load local sound
Write the code at the first frame:
Code
// The URLRequest class is used to specify the address to be loaded. The address is a local address.
Var url: URLRequest = new URLRequest (string url );
// Declare a sound object
Var sound: Sound = new Sound ();
// Load the url-loaded Sound into the Sound object using the load Method
Sound. load (url );
// Play
Sound. play ();
3. Load remote sound
Write the code at the first frame:
Code
// The URLRequest class is used to specify the address to be loaded. The url is the http address.
Var url: URLRequest = new URLRequest (string url );
// Declare a sound object
Var sound: Sound = new Sound ();
// Load the url-loaded Sound into the Sound object using the load Method
Sound. load (url );
// Play
Sound. play ();
4. Loading progress
First, you must know how the progress is calculated.
Formula: Progress = loaded Bytes/total number of bytes
Now we need to know how to obtain the loaded data and how to obtain the total number of bytes.
1. Obtain the sound attributes.
Sound. bytesLoaded: obtains the number of loaded bytes.
Sound. bytesTotal: obtains the total number of bytes.
2. Add a new layer named "display progress" and add a dynamic text named jd_txt at the first frame.
3. Write the code at the first frame of the as layer:
Code
// The URLRequest class is used to specify the address to be loaded. The url is the http address.
Var url: URLRequest = new URLRequest ("http: // 121.28.24.122/blog/eb/upload/audio1/20090422/hahaxiao_1240364607390.mp3 ");
// Declare a sound object
Var sound: Sound = new Sound ();
// Load the url-loaded Sound into the Sound object using the load Method
Sound. load (url );
// Add loading events to the sound object
Sound. addEventListener (ProgressEvent. PROGRESS, jz );
// Add events after the sound object is loaded
Function jz (e ){
Var jd: Number = Math. round (sound. bytesLoaded/sound. bytesTotal) * 100 );
Jd_txt.text = "loading" + jd + "% ";
}
The effect of the test video is as follows:
Knowledge point:
1. For methods, attributes, and events of a Sound object, there is no need to explain too much about methods and attributes. The most important thing is the event.
The events of the Sound object are as follows:
The above table shows five events of the Sound object. The most common events are complete, ioError, and progress events.
In the above column, we use the progress event. We add an event through the addEventListener method of the Sound object. There are two parameters,
One is ProgressEvent. PROGRESS, the other is jz, the first parameter is the event type, and the second is the event execution function.
When defining the jz function, note that there must be a parameter e, which is the information related to the event generation. This function can be written in two ways:
Function jz (e ){
....
}
// Or
Function jz (Event: ProgressEvent ){
....
}
Which one is the same.
This is the end of today. Next, let's add a progress bar!