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:
  • Maxie in A Munin plugin to track http response times. on

    Nice Plug in

  • Dave in A Munin plugin to track http response times. on

    Nice plugin, it works really well. When clicking through to the graphs I get:

    "NOTE! This service is in CRITICAL state because one of the values reported is outside the ...

  • Woody Smith in Loading Reloaded on

    Definitely, this class is very helpful to those looking for a very effective loading system. We are developing a new project and I definitely think that this would be a ...

  • Björn in A Munin plugin to track http response times. on

    Hi,

    I've installed the plugin and it seems to run correctly but the graphs are empty. No data. Got any ideas?

    Munin and all my other customized plugins is working ...

  • Raweden in The Web does not need flash. on

    interesting reading, for long Flash have been in it's dying days. Everybody talking about HTML5 & JavaScript is the new web, which i find kind of ugly. I think almost ...

Feeds: Entries rss feed Linksrss feed Worksrss feed

A Django joint. hosted on Slicehost