?
Solved

AS3 : Preloading mulitiple flvs

Posted on 2010-04-04
13
Medium Priority
?
579 Views
Last Modified: 2013-11-11
Hello,

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!

Cheers!


0
Comment
Question by:cougarsy
  • 8
  • 5
13 Comments
 

Author Comment

by:cougarsy
ID: 29644623
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,
0
 
LVL 19

Expert Comment

by:Gary Benade
ID: 29875673
If you are just playing audio you should be able to use a flash.media.Sound object instead

Have you considered using BulkLoader as a preloader instead?

http://code.google.com/p/bulk-loader/
0
 

Author Comment

by:cougarsy
ID: 29881072
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!
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 19

Accepted Solution

by:
Gary Benade earned 2000 total points
ID: 29896842
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 } );                  
bulkLoader.start();

// 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
0
 

Author Comment

by:cougarsy
ID: 29902240
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.

Cheers!
0
 
LVL 19

Expert Comment

by:Gary Benade
ID: 29905827
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( 'http://foo.bat.com/pic1.jpg', { id:'pic1' } );

and use it later as:

bulkLoader.getBitmap( 'pic1', true) as Bitmap;
0
 

Author Comment

by:cougarsy
ID: 29907633
Hi,

This statement:

bulkLoader.numConnections = 3;

threw an error:  ReferenceError: Error #1074: Illegal write to read-only property numConnections on br.com.stimuli.loading.BulkLoader.

So, I changed my code to:

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

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

This doesn't throw an error, but I still don't get any correct bytes or percentages when it is loading....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 br.com.stimuli.loading.BulkLoader;
import br.com.stimuli.loading.BulkProgressEvent;
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(e.target.data);
		var bulkLoader=new BulkLoader("testassets",3);
		xmlItemNum=myXML.question.length();
		trace(xmlItemNum);
		for (var i:Number=0; i<= xmlItemNum-1; i++) {
			xmlString=audioLocation+myXML.question[i].flv;
			trace(xmlString);
			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;
		bulkLoader.start();
		function onAllItemsLoaded(evt : Event):void {
			trace("every thing is loaded!");
			var video : Video = new Video();
			var ns : NetStream = bulkLoader.getNetStream("flv49");
            addChild(video);
            video.attachNetStream(ns);
            ns.resume();
		}
		
		function onAllItemsProgress(evt : BulkProgressEvent):void {
			trace("In Progress function!");
			percentLoadedField = new TextField();
			addChild(percentLoadedField);
		    var percentText:Number=evt.bytesLoaded/evt.bytesTotal;
			percentLoadedField.text = String(Math.round(percentText*100));
 trace( 'percent loaded', evt.percentLoaded);
			trace(evt.loadingStatus());
		}	
	}
}

// calls loadXML
loadXML();

Open in new window

0
 

Author Comment

by:cougarsy
ID: 29908799
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!
0
 
LVL 19

Expert Comment

by:Gary Benade
ID: 29910091
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);
or
 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

Regards
Gary

0
 

Author Comment

by:cougarsy
ID: 29914654
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!
0
 
LVL 19

Expert Comment

by:Gary Benade
ID: 29914967
the progress can be a bit dodgy but you can relax, if the complete event fires then everything was loaded :)
0
 

Author Comment

by:cougarsy
ID: 29915845
Thank you very much!

Cheers,

0
 

Author Closing Comment

by:cougarsy
ID: 31710682
Very complete solution!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question