SoFunction
Updated on 2025-03-09

Make a full-featured FLV player

This article is selected from the book "Flash MX Professional 2004 First Step"
Author Chen Bing

Create a full-featured FLV player

The FLV player we will make consists of several parts:
A video object for displaying video images,
Three buttons for playing, pausing and stopping videos,
and a dynamic text for displaying the buffer loading progress.

1. Create three button components, named "Play", "Pause", and "Stop", and drag and drop one of their respective instances into the scene stage.

2. Create a video component, drag and drop one of its instances into the scene stage, and give it the instance name myVideo.

3. Place a dynamic text on the scene stage and give its example name myText.

4. Bundle the following script in frame 1 of the root timeline:

Copy the codeThe code is as follows:

//Create a NetConnection object.
myFLVConnection=new NetConnection(); 
//Create a stream connection.
(null); 
//Create a NetStream object.
//myFLVConnection is assigned to the NetStream object.
myFLVStream=new NetStream(myFLVConnection); 
//myFLVStream is bundled to the Video object myVideo:
(myFLVStream); 
//Set the buffer time.
(10); 
To analyze this script, first I created a NetConnection object myFLVConnection, and then I called the connect() method of the NetConnection object to open a stream connection. When calling the connect() method you have to pass it a null value as a parameter.

Next, I use myFLVStream=new NetStream(myFLVConnection); This line of code creates a NetStream object myFLVStream. When I create it, I need to inform it of the "connection" provided for the "stream", that is, pass the myFLVConnection object as a parameter to the constructor of the NetStream class.
After that, I bundled myFLVStream ("stream") to the Video object myVideo. Then, I also used the setBufferTime() method of the NetStream class to specify a buffer time in seconds, that is, how long is the data available for playback to be loaded into the buffer before starting playback.

5. Bundle the following script on the "Play" button instance:

Copy the codeThe code is as follows:

on(release){ 
//Load and play FLV files.
    (""); 

//Define the bufferLoad function for setInterval function call to display the buffer progress.
    function bufferLoad(){ 
//Show the buffer loading progress in dynamic text.
="The buffer is loaded"+int((/)*100)+"%";

//Set the time interval.
        setInterval(bufferLoad,20); 

This line of code—(""); is used to load and play FLV files. If your FLV file is located somewhere in an HTTP address or local file system, you can use a format like http:// or file:// as the prefix of the path to specify the location of the FLV file.
This expression—/ is used to check the ratio of the loaded bytes in the buffer to the total bytes to be loaded by the buffer. bytesLoaded and bytesTotal are two properties of the NetStream class.

6. Bundle the following script on the "Pause" button instance:

Copy the codeThe code is as follows:

on(release){ 
//Pause FLV file.
    (); 

7. Bundle the following script on the "Stop" button instance:

on(release){ 
//Stop the FLV file and delete the downloaded FLV file at the same time.
    (); 

8. Completed production. You should test this FLV player in your website.