View comments | RSS feed

Preloading FLV files

To track the downloading progress of FLV files, use the NetStream.bytesLoaded and NetStream.bytesTotal properties. To obtain the total bytes and current number of bytes loaded for an FLV file, use the NetStream.bytesLoaded and NetStream.bytesTotal properties.

The following example uses the bytesLoaded and bytesTotal properties that show the loading progress of video1.flv into the video object instance called my_video. A text field called loaded_txt is dynamically created to show information about the loading progress.

To preload an FLV file:

  1. Create a new FLA file called preloadFLV.fla.
  2. In the Library panel (Window > Library), select New Video from the Library
    pop-up menu.
  3. In the Video Properties dialog box, name the video symbol and select Video (ActionScript controlled).
  4. Click OK to create a video object.
  5. Drag the video object from the Library panel to the Stage to create a video object instance.
  6. With the video object selected on the Stage, type my_video in the Instance Name text box in the Property inspector (Window > Properties > Properties).
  7. With the video instance still selected, type 320 in the width text box and 213 in the height text box in the Property inspector.
  8. Select Frame 1 in the Timeline, and open the Actions panel (Window > Actions).
  9. Type the following code in the Actions panel:
    var connection_nc:NetConnection = new NetConnection();
    connection_nc.connect(null);
    var stream_ns:NetStream = new NetStream(connection_nc);
    my_video.attachVideo(stream_ns);
    stream_ns.play("http://www.helpexamples.com/flash/video/lights_short.flv");
    
    this.createTextField("loaded_txt", this.getNextHighestDepth(), 10, 10, 160, 22);
    var loaded_interval:Number = setInterval(checkBytesLoaded, 500, stream_ns);
    function checkBytesLoaded(my_ns:NetStream) {
        var pctLoaded:Number = Math.round(my_ns.bytesLoaded / my_ns.bytesTotal * 100);
        loaded_txt.text = Math.round(my_ns.bytesLoaded / 1000) + " of " + Math.round(my_ns.bytesTotal / 1000) + " KB loaded (" + pctLoaded + "%)";
        progressBar_mc.bar_mc._xscale = pctLoaded;
        if (pctLoaded >= 100) {
            clearInterval(loaded_interval);
        }
    }
    
  10. Select Control > Test Movie to test your code.

    NOTE

     

    If your progress bar loads instantly, the video has cached on your hard disk (either from testing this example or loading it in a different procedure). If this occurs, upload a FLV file to your server and load it instead.

Another way to preload FLV files is to use the NetStream.setBufferTime() method. This method takes a single parameter that indicates the number of seconds of the FLV stream to buffer before playback begins. For more information, see setBufferTime (NetStream.setBufferTime method), getBytesLoaded (MovieClip.getBytesLoaded method), getBytesTotal (MovieClip.getBytesTotal method), bytesLoaded (NetStream.bytesLoaded property), bytesTotal (NetStream.bytesTotal property), and setInterval function in the ActionScript 2.0 Language Reference.


Flash CS3


Comments


rollingsj said on Oct 24, 2007 at 11:35 AM :
This is pretty sloppy documentation. The code above references progressBar_mc, but doesn't describe where or how this is created. Also, wouldn't it make more sense to play the move after it has loaded instead of immediately? Isn't that the whole point of preloading? Otherwise the movie will start playing, look choppy, pause, load some more, start playing, look choppy, repeat. Incidentally, this is what the example video does.
No screen name said on Dec 14, 2007 at 1:01 PM :
The example above is fine; it's for a "download progress bar" or "loading progress bar", not a pre-loader (though that's the title), and would be used in a media player as one of two bars; one to show playhead position (stream_ns.time) and a second bar (this example) to show how much has downloaded . Choppy playback would be controlled via the buffer anyway with...

stream_ns.setBufferTime(7);

...and is typically due to an encoding rate that is too high for the capacity of most end-users (like anything above 350Kbps). Come on, who wants to wait for a video to download "before" you start playing it?
=MANTA= said on Jul 29, 2008 at 1:07 PM :
actually what I have been searching for and would have liked to have seen is a practical example of how to abort a progressive download so that it doesn't lock up the whole browser until it completes?

Please feel free to email me ron@moviegoods.com with any examples that could help me with this.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001036.html