Preloading Video in AS2

Quite obvious actually, but it's easy to preload NetStreams and use them as needed, without having to tie them to NetStream objects upfront.

Sometimes you need to preload a whole video before displaying it. Maybe you will use it as a transition, maybe it's integrated into the user interface(it's part of a menu, for example). Because videos must be attached to Video instances, you need to have you movie clip hierarchy in place so you can attach the NetStream and load it. This imposes too many restraints on how you must have your clips setup (and instantiated) before loading.

It's pretty obvious, but it took me a while to figure this one out: you don't need a Video instance to preload videos. For example, suppose you have a menu with three items and a video to play for each. No need to keep different videos on stage. You can preload the stream directly. It's so simple in fact that there's barely code to show.

1
2
3
4
5
main_conn = new NetConnection();
main_conn.connect(null);

main_stream = new NetStream(main_conn);
main_stream.play(url);

From there you can control loading easely through main_stream.bytesTotal and main_stream.bytesLoaded. When you need to play that video, just attch the NetStream to it: video.attachVideo(main_stream);.

As an added bonus, after playing your video, you can get rid of your Video instance (remove from the stage) and change the movie clip hierarchy as you see fit. Later on, just attach another Video instance and call video.attachVideo(main_stream); on it, and voilá, you can safely keep you preloaded stream, with two caveats:

  • You'll be keeping the whole stream in memory, so this will work better for smaller videos (You don't want to waste your user's memory preloading large videos)
  • You don't call close() on the NeStream, if you do, it will be gone from memory.

The same trick works for AS3 too. Even though with the URLLoader you could always use the raw data as cast it to a NetStream, it's pretty handy.

posted by
Arthur Debert

on

Tagged with:

It's quiet here.
Say something

Other months availabe in 2006

The complete blog archive

The latest entries

Subscribe to comments on this entry: rss feed

Cloud me:
  • Fabianv in Hosting: Life is good again on

    I had the same thing happen to me.. luckily I realised going dedicated was going to save a lot of stress and time sooner than you did.

    I started ...

  • Rajiv Ranjan Kumar in Email validation in Actionscript 3 on

    Hi Dear,

    Thank you for this solution.

    Rjiv Ranjan Kumar

  • Jeff in Why Hashes? on

    hi,

    could you show an example of ho the method looks that is being called by playSound("rollover", {loops:true}); ?

    I think a final exampe of that is missing, ...

  • Viktor in BulkLoader :: Refactor me on

    Hi,

    very useful indeed.

    Some of the resource files we use in our server side need encoding/decoding time that means the server sends back a percentage and if ...

  • Arthur Debert in Tweener Tips on

    Hi There.

    Tweener has a great mailing list which is a better place to get help than this blog entry.

    Cheers Arthur

Feeds: Entries rss feed Linksrss feed Worksrss feed

A Django joint. hosted on Slicehost