AS3 : Preloading mulitiple flvs


I have 55 flvs that I would like to preload so they are available for play when I need them in an online test.  The code below successfully loads 2 of the flvs, but I have some questions before proceeding:

--should I have 1 netstream per flv?

I believe that is what is needed, preload and position them at 0 and then I should be able to play them later.  Would I also need 55 Video objects as well (even though at this point the flvs are sound only)....But, then another question:

--in AS3, how do I create variable names on the fly, which I did in AS2 like this for example for loadVars():

_root[nyData+i] = new loadVars();

Thanks a lot!


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cougarsyAuthor Commented:
Hello again,

OK, have I believe answered my questions with 1 netstream per flv and named like this:

this["ns"+i] = new NetStream(this["nc"+i]);

Another question though:

If the flv is only audio, do I need to attach each netstream to its own Video object?  I am following an example I found on a forum, but I was thinking that I shouldn't need a Video object if the flvs are just audio?  

Can someone please confirm if my logic is correct?

Many thanks again,
Gary BenadeCommented:
If you are just playing audio you should be able to use a object instead

Have you considered using BulkLoader as a preloader instead?
cougarsyAuthor Commented:
Hi hobbit72,

Thank you for your response.  I actually stumbled upon BulkLoader yesterday.  Do you have experience with BulkLoader?  From what I can see with BulkLoader I can set up all to load and assign them an id and then play them when I need later in the movie.  It seems like a good solution for what I would like to do though I haven't actually tried it out yet.  

All of my sounds are flvs.....does the Sound object load and handle flvs-- I see examples with mp3s but wasn't sure if it would accept flvs?  Also, is there any reason from a performance perspective to choose the Sound object over Video?

Many thanks again!
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Gary BenadeCommented:
There will be no performance penalty to using Video instead of Sound

Yes, I have used bulkloader many times, it's really good stuff

// this need to be visible from everywhere you need to use it, so I put it in a singleton ModelLocator class
public var bulkLoader:BulkLoader;

// and then I queue the stuff up in my application preload sequence
bulkLoader = new BulkLoader("videoassets");
bulkLoader.addEventListener(BulkLoader.COMPLETE, onVideoAssetsLoaded);  // in this function I fire an event to let the application know that it's time to start
bulkLoader.add( "http://puglobal.localhost/flash/video/en/PuWebMood v14 20080529.f4v", { id:'homevideo', pausedAtStart:true } );
bulkLoader.add( "http://puglobal.localhost/flash/video/en/PilsnerGolfIntrov4 20080529.f4v", { id:'puregolfvideo', pausedAtStart:true } );                  

// note the pausedAtStart parameter, without it all the video start playing as soon as they are loaded

and then later on when you need them you just reference them as follows:
var ns:NetStream = bulkLoader.getNetStream( 'homevideo', false) as NetStream;  // you reference a video with the id you passed to bulkloader

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cougarsyAuthor Commented:
Hi hobbit72,

Thanks again for your response.  I have done as you suggested and it seems to work for the most part.  However, I do have a couple questions:

--total bytes is always 0 and bytes loaded varies (sometimes it shows a value and sometimes 0) yet it does show that all 59 assets were loaded and I can play them.  But, I'd like to have a progress bar and so do you know why the bytes are always 0 and/or has varying values for the same set of files?  How can I implement a progress bar with these values?

--I will also load bitmaps.  I am still a newbie to AS3 and Bulkloader....can you please point me in the right direction as to what to do with the Bitmaps once they are loaded by Bulkloader so that they are available but no visible until I need them?

Thanks again for your patience.

Gary BenadeCommented:
The progress can be erratic if you don't limit the amount of connections bulkloader can make before you call start():

bulkLoader.numConnections = 3;

You should be able to get a more accurate progress by using.

bulkLoader.addEventListener(BulkLoader.PROGRESS, onProgress);

private function onProgress( evt:BulkProgressEvent) :void
    trace( 'percent loaded', evt.percentLoaded);

to load images just queue them as usual

bulkLoader.add( '', { id:'pic1' } );

and use it later as:

bulkLoader.getBitmap( 'pic1', true) as Bitmap;
cougarsyAuthor Commented:

This statement:

bulkLoader.numConnections = 3;

threw an error:  ReferenceError: Error #1074: Illegal write to read-only property numConnections on

So, I changed my code to:

var bulkLoader=new BulkLoader("testassets",3);

I believe that set up 3 connections least I hope so.

This doesn't throw an error, but I still don't get any correct bytes or percentages when it is this perhaps due to the fact I am testing locally?

Your line of code is also always 0 ("percent loaded").

Also, it only traces evt.loadingStatus() 4 times for loading 59 files (meaning it only gets into the function "onAllItemsProgress" 4 times), so don't know if this is normal or not.

I will attach my code.  Thanks again for your help.

import ModelLocator;
var audioLocation:String="Audio/";
var jpgLocation:String="Pics/";
var xmlString:String;
var xmlItemNum:Number;
var totalBytes:Number=459072;
var flvNum:Number=59;
var xmlFile:String="test.xml";
var myXML:XML;
var loaded_interval:Number;
var flvMarker:Number=0;
var customClient:Object = new Object();
var percentLoadedField:TextField;
var myConstants:ModelLocator = new ModelLocator();


//***********LOAD XML and FLVs*******************************//

function loadXML() {
	var myLoader:URLLoader = new URLLoader();
	myLoader.load(new URLRequest(xmlFile));
	myLoader.addEventListener(Event.COMPLETE, processXML);
	function processXML(e:Event):void {
		trace("in processXML!");
		myXML=new XML(;
		var bulkLoader=new BulkLoader("testassets",3);
		for (var i:Number=0; i<= xmlItemNum-1; i++) {
			bulkLoader.add(xmlString,{maxTries:6, id:"flv"+i, pausedAtStart:true});
			trace("one added!");
		bulkLoader.addEventListener(BulkLoader.COMPLETE, onAllItemsLoaded);
		bulkLoader.addEventListener(BulkLoader.PROGRESS, onAllItemsProgress);
		//bulkLoader.numConnections = 3;
		function onAllItemsLoaded(evt : Event):void {
			trace("every thing is loaded!");
			var video : Video = new Video();
			var ns : NetStream = bulkLoader.getNetStream("flv49");
		function onAllItemsProgress(evt : BulkProgressEvent):void {
			trace("In Progress function!");
			percentLoadedField = new TextField();
		    var percentText:Number=evt.bytesLoaded/evt.bytesTotal;
			percentLoadedField.text = String(Math.round(percentText*100));
 trace( 'percent loaded', evt.percentLoaded);

// calls loadXML

Open in new window

cougarsyAuthor Commented:
FYI this is the trace output:

In Progress function!
percent loaded 0
BulkProgressEvent bytesLoaded: 371779, bytesTotal: 0, itemsLoaded: 2, itemsTotal: 59, bytesTotalCurrent: 0, percentLoaded: 0, weightPercent: 0.05, ratioLoaded: 0.03;
In Progress function!
percent loaded 0
BulkProgressEvent bytesLoaded: 415313, bytesTotal: 0, itemsLoaded: 2, itemsTotal: 59, bytesTotalCurrent: 0, percentLoaded: 0, weightPercent: 0.07, ratioLoaded: 0.03;
In Progress function!
percent loaded 0
BulkProgressEvent bytesLoaded: 5900991, bytesTotal: 0, itemsLoaded: 45, itemsTotal: 59, bytesTotalCurrent: 0, percentLoaded: 0, weightPercent: 0.78, ratioLoaded: 0.76;
In Progress function!
percent loaded 0
BulkProgressEvent bytesLoaded: 5900991, bytesTotal: 0, itemsLoaded: 59, itemsTotal: 59, bytesTotalCurrent: 0, percentLoaded: 0, weightPercent: 0.78, ratioLoaded: 1;
every thing is loaded!
Gary BenadeCommented:
Yes, you are correct, you set the number of connections in the constructor

Try using the evt.weightPercent or evt.ratioLoaded value of the BulkProgressEvent in the event handler instead

 trace( 'percent loaded', evt.ratioLoaded * 100.0);
 trace( 'percent loaded', evt.weightPercent * 100.0);

You should see more progress events if you test this live. It is timer based at set intervals and locally it is all happening too fast to be meaningful


cougarsyAuthor Commented:
Thanks a lot.  I will give it a try.  Do you think that even with the bytes loaded being off, I can assume that it did really load all of the assets correctly?  It does say 59 items loaded and that is correct.  It's just that I have never used BulkLoader and wouldn't want to find out later that it didn't really load all of them.

Thanks again for your very complete answers and help!
Gary BenadeCommented:
the progress can be a bit dodgy but you can relax, if the complete event fires then everything was loaded :)
cougarsyAuthor Commented:
Thank you very much!


cougarsyAuthor Commented:
Very complete solution!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.