Tip: Homemade Flash FLV video Player

Source: Internet
Author: User
Tags current time file size window flv file
Skills | Video Playback Everyone good, not long ago I will cenfun Music player to join the FLV video playback function, here alone to give everyone to do a flash FLV player (ActionScript only control, do not use FLVPlayback components), If there is any improper place also to correct.
Let's turn on your Macromedia Flash Player 8 (recommended) Start!

First create a new document, set the background color black, other defaults, and then build four layers.

The first layer is used to put video components, as follows:

1, in the Library Panel (Window > Library), select New video from the Library pop-up menu.
2, in the Video Properties dialog box, name the video symbol and select "Video" (controlled by ActionScript).
3, drag the video object from the library panel to the center of the stage to create a video object instance.
4, take an instance of this video symbol named "My_video".

The second layer is used to put the video address input bar as follows:

1, in the lower left of the stage with the Text tool (shortcut key T) to draw an address input text box, type select "Input text" type.
2, select Single row in the line type pop-up menu, and make sure that the show borders around text is selected.
3, take the instance of this text box named "url".

The third level is used to put the play Start button as follows:

1, in the Library Panel (Window > Library), create a new Component button, the button style is made by itself, temporarily can be used on the line.
2, drag the New button object from the library panel to the Stage Address entry box to create the play Start button.
3, for this play Start button take the instance named "Play_bt".

Layer fourth is used to put all ActionScript:

Initialize first
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 Play function playflv ()
function playflv (flv) {
Parameter flv is the FLV video address to play
Trace (flv);//test with
Attach the NetStream video input signal to the video object, which is the visual component My_video
My_video.attachvideo (My_ns);
Set buffer time, in seconds, set 3 seconds below
My_ns.setbuffertime (3);
Start playing FLV files
My_ns.play (FLV);
}
Click the Start Play button to start playing
Play_bt.onrelease = function () {
playflv (Url.text);
Get the video file address of the URL input box and call the play function to play the URL corresponding to the FLV video file
}; //////////////////////////////////////////////////////////////////////////////
This is the simplest player has done, the following is to do more control and performance work.
Here on its several important aspects of production, others also need to play their own imagination to make better design.
Note that the following code is not necessary, untested, I hope everyone one try to achieve. Pay special attention to the correspondence between the path and the instance name.

//*********************************
1, playback of the control, pause and stop the implementation
Create a new Two button, a pause (PAUSE_BT), a Stop (STOP_BT), and the same principle as the play button.

Pause_bt.onrelease = function () {
My_ns.pause ();
};
Stop_bt.onrelease = function () {
My_ns.seek (0);
Search starting from 0
My_ns.pause (TRUE);
The argument true indicates a pause, and if False indicates that it has changed from paused to resume playback, or to toggle in Suspend/play if there are no arguments.
};//*********************************
2, Video download progress
This is relatively simple, and similar to the general download progress, the principle is to play the downloaded and the total file size percentage, and then displayed.
Creates a new static text (info) and progress bar (bar) that displays percentages, and adjusts its initial state and position itself

This.onenterframe =function () {
var loadedbytes = my_ns.bytesloaded;
Get already 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 more important, because the proportion of video size is generally different, so play to adjust to avoid distortion deformation.
The principle is to get the FLV size, then resize the My_video, and finally center the position, and if necessary, zoom processing (omitted here).

First write a change dimension function Changesize (W, h), W for the width to be changed to, h for the height to be changed
function Changesize (W, h) {
Change to the parameter dimensions that are passed in
My_video._width = W;
My_video._height = h;
Trace ("W:" +w+ "H:" +h);
Position Center processing, if your video stage width 550, high 400
my_video._x = 550/2-W/2;
my_video._y = 400/2-H/2;
}
Then get the intrinsic size of the FLV and call the above function to change it
This handler fires after the My_ns.play () method is invoked and before the video playhead advances
My_ns.onmetadata = function (infoobject:object) {
Get the descriptive information embedded in the FLV file, where you get a wide/high
var flv_width = infoobject.width;
var flv_height = infoobject.height;
Change size
Changesize (Flv_width, flv_height);
}; //*********************************
4, Play time and progress
The principle is similar to the download progress, you get the total duration, then you get the current time percentage, or you can make a progress bar.

Defines the total duration global variable and obtains its value.
var flv_duration;
My_ns.onmetadata = function (infoobject:object) {
Gets the descriptive information embedded in the FLV file, where the total duration is obtained (in seconds)
var flv_duration = infoobject.duration;
};
Note: This can be written together with the get-wide height.
Get Current playback time
var flv_thistime = My_ns.time;
Then you can make the playback progress, and download progress similar, everyone make their own, here slightly. //*********************************
5, Volume control

This is a bit complicated, you must first attach the audio from the FLV file to the movie clip on the stage, and then control it
Create a new movie clip My_ns_mc and attach audio
My_ns_mc.attachaudio (My_ns);
Create a new Sound object for a movie clip
var my_ns_sound = new Sound (MY_NS_MC);
Initialize Volume (default 80 here)
var flv_volume = 80;
My_ns_sound.setvolume (Flv_volume);
Finally, the size of the Flv_volume (between 0 and 100) can be controlled to change the volume size
This part of the production is also omitted, everyone free play, can also make mute function, that is, Flv_volume 0//*********************************
Also, like fast forward, rewind, buffer display, etc., can be achieved, we study.

////////////////////////////////////////////
Finally, the production of a flash FLV player is basically finished, recommend want to learn flash friends hands-on to achieve, do not always want to download what source code and then modify a pass, that will not realize that many of the mysteries of it!

Add a related question: FLV playback has no image, only sound, because this FLV file is compressed in Flash 8 encoding format, and you release Flash player for Flash 7 or lower version, so upgrade to version 8, or the FLV file using Flash 7 encoding format compression.

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.