I have previously studied flash com with Liu Gong. Today I wrote an example about flash com. I want to learn how to use it. It is a simple example of playing FLV videos in flash ..
Start with Macromedia Flash Player 8 (recommended!
Create a new document, set the background color to black, and create four layers by default.
The first layer is used to place video elements. The method is as follows:
1. on the "Database" Panel ("window"> "Database"), select "new video" from the "Database" pop-up menu ".
2. In the "video properties" dialog box, name the video component and select "video" (controlled by the ActionScript ).
3. Drag the video object from the "Database" panel to the center of the stage to create a video object instance.
4. Name the video element "my_video ".
The second layer is used to place the video address input column. The method is as follows:
1. Use the text tool (shortcut key t) at the bottom left of the stage to draw an address input text box and select "input text" as the type.
2. Select "single line" from the "line type" pop-up menu, and confirm that "show border around text" is selected.
3. For this text box, name the instance "url ".
The third layer is used to play the start button. The method is as follows:
1. on the "Database" Panel ("window"> "Database"), create a component button and create a button style. You can use it for the moment.
2. Drag the new button object from the "Database" panel to the end of the stage address input box to create the play start button.
3. Name the instance play_bt for the start button ".
The fourth layer is used to put all the ActionScript:
// Initialize
// Create a netconnection object
VaR my_nc: netconnection = new netconnection ();
// Create a local stream connection
My_nc.connect (null );
// Create a netstream object
VaR my_ns: netstream = new netstream (my_nc );
// Write a playflv function ()
Function playflv (FLV ){
// The FLV parameter indicates the FLV video address to be played.
// Trace (FLV); // used for testing
// Append the netstream video input signal to the video object, that is, the video element my_video.
My_video.attachvideo (my_ns );
// Set the buffer time, in seconds. Set it to 3 seconds.
My_ns.setbuffertime (3 );
// Start playing FLV files
My_ns.play (FLV );
}
// Click start playback to start playing.
Play_bt.onrelease = function (){
Playflv (URL. Text );
// Obtain the video file address in the URL input box and call the playback function to play the FLV video file corresponding to the URL.
};
//////////////////////////////////////// //////////////////////////////////////
// At this point, the simplest player has been completed, and more control and performance work is required.
// Here we will make several important aspects. Others need to make better designs based on your imagination.
// Note that the following code is not necessary and has not been tested. We hope you can try it one by one. Pay special attention to the corresponding path and Instance name.
//*********************************
// 1. Playback control, pause, and stop
// Create two buttons, pause_bt and stop_bt. The principle is the same as that of the playback button.
Pause_bt.onrelease = function (){
My_ns.pause ();
};
Stop_bt.onrelease = function (){
My_ns.seek (0 );
// Search for playing from 0
My_ns.pause (true );
// If the parameter is true, it indicates that the video is paused. If the parameter is false, it indicates that the video is paused and continues to be played. If the parameter is not set, it indicates that the video is paused or played.
};
//*********************************
// 2. video download progress
// This is relatively simple. It is similar to the general download progress. The principle is to display the percentage of downloaded and total file sizes during playback.
// Create a new display percentage static text (Info) and a progress bar, and adjust its initial status and position
This. onenterframe = function (){
VaR loadedbytes = my_ns.bytesloaded;
// Get the downloaded bytes
VaR totalbytes = my_ns.bytestotal;
// Total file size
If (totalbytes = undefined | totalbytes <4000 ){
Info. Text = "0% ";
Bar. _ width = 1;
} Else {
VaR nowloadpercent = math. Round (loadedbytes/totalbytes * 100 );
If (isnan (nowloadpercent )){
Info. Text = "0% ";
Bar. _ width = 1;
} Else {
Info. Text = nowloadpercent + "% ";
Bar. _ width = nowloadpercent * 35/100;
If (nowloadpercent = 100 ){
Delete this. onenterframe;
}
}
}
}
//*********************************
// 3. Video size correction or adjustment
// This is important because the video size ratio is generally different, so adjust the video size during playback to avoid distortion and deformation.
// The principle is to obtain the FLV size, re-adjust the my_video size, center the position, and perform scaling if necessary (omitted here ).
// First, write a function changesize (W, H) for changing the size. W is the width to be changed, and H is the height to be changed.
Function changesize (W, h ){
// Change the size of the input parameter
My_video. _ width = W;
My_video. _ Height = h;
// Trace ("W:" + W + "H:" + H); // used for testing
// Center the video. If your video stage is 550 wide and 400 high
My_video. _ x = 550/2-W/2;
My_video. _y = 400/2-H/2;
}
// Then obtain the inherent FLV size and call the above function to change it
// This processing function is triggered before the video playback header advances after the my_ns.play () method is called.
My_ns.onmetadata = function (infoobject: Object ){
// Obtain the descriptive information embedded in the FLV file. The width and height are obtained here.
VaR flv_width = infoobject. width;
VaR flv_height = infoobject. height;
// Change the size
Changesize (flv_width, flv_height );
};
//*********************************
// 4. Playback time and progress
// The principle is similar to the download progress. First, get the total duration, and then get the percentage of the current time. You can also make a progress bar.
// Define the global variable for the total duration and obtain its value.
VaR flv_duration;
My_ns.onmetadata = function (infoobject: Object ){
// Obtain the descriptive information embedded in the FLV file. The total duration (in seconds) is obtained here)
VaR flv_duration = infoobject. duration;
};
// Note: this can be written together with the obtained width and height.
// Obtain the current playback time
VaR flv_thistime = my_ns.time;
// Then you can make the playback progress, which is similar to the download progress.
//*********************************
// 5. Volume Control
// For this complexity, you must first attach the audio from the FLV file to the video clip on the stage and then control it.
// Create a video clip my_ns_mc and append the audio
My_ns_mc.attachaudio (my_ns );
// Create a sound object for a video clip
VaR my_ns_sound = new sound (my_ns_mc );
// Initialize the volume (80 by default)
VaR flv_volume = 80;
My_ns_sound.setvolume (flv_volume );
// Adjust the volume of flv_volume (between 0 and 100) to change the volume.
// This part of production is also omitted. You can use it as needed. You can also use the mute function, that is, the flv_volume value is 0.
//*********************************
// Also, such as fast forward, fast return, and buffer display can be implemented. You can study it on your own.
//////////////////////////////////////// //////////////////////////////////////
Finally, the preparation of a flash flvplayer is basically complete. We recommend that you learn the Flash Player and do not always download the source code and modify it, in this way, you will not be able to understand many of the secrets!
Add a related question: FLV playback has no image and only sound. This is because the FLV file is compressed in Flash 8 encoding format, and the flash player you released is Flash 7 or earlier, upgrade to version 8 or compress the FLV file in the Flash 7 encoding format.