Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
BGoulette

Loading a video via XML file?

Recommended Posts

Hi!

 

I'm probably doing it wrong, but I'm trying to load, in addition to a slew of other XML-related stuff, a video through an XML file.

 

Am I wrong to try to do something like this?

 

lm=new LoaderMax({name:"main", onError:onError, onProgress:onProgress, onComplete:onComplete});
sl=new SelfLoader(target);
xl=new XMLLoader("filename.xml", {name:"xmlContent"});

lm.append(sl);
lm.append(xl);

lm.load();
// Not shown are the event listeners

 

Within the XML file, buried among some other nodes, I have:

 


 

When I trace out LoaderMax.getContent("vidLoader") or LoaderMax.getContent("commercial"), I get null.

 

What am I doing wrong? :( Thanks!

Link to post
Share on other sites

Sounds like you just forgot to activate() VideoLoader so that XMLLoader can recognize those nodes. Remember, LoaderMax was engineered in a way that allows for smaller files - most other loading systems cram ALL the code into the main loader class so that even if you're only loading text files, all the code for handling video, swfs, images, sounds, etc. are all compiled into your swf. I think that's wasteful. LoaderMax is more efficient, but the only cost to this efficiency is that you've got to activate() the necessary loaders once in your swf to make sure the compiler includes them. (kinda like the way you must activate() plugins for use in TweenLite/Max).

 

In short, all you need to do is add this one line of code:

 

LoaderMax.activate([VideoLoader]); 

 

Obviously if your XML contains other types of nodes like , , etc., you should activate those too.

 

Does that clear things up for you?

Link to post
Share on other sites
Does that clear things up for you?

 

Why yes, yes it does! And I agree: I like that LoaderMax doesn't try to cram everything and its grandma into memory without your permission! Of course, when you forget to give it permission... :(

 

I guess I thought that because I created an XMLLoader with just xl=new XMLoader("blah.xml", {name:"blah"}), I could get away with it. Of course I overlooked the fact that I'm "manually" instantiating the XMLLoader instance while relying on LoaderMax to read my mind re: the VideoLoader instance in the XML file!

 

Now to just figure out how to attach/manipulate the loaded video! Thanks for the quick reply!

Link to post
Share on other sites

One (ha!) more thing:

 

I feel stupid, and I apologize for wasting your time, but after I've loaded a video through a VideoLoader instance, I'm trying to figure out what to assign it to: for example, with an XMLLoader, I can do the following: loadedXML=new XML(LoaderMax.getContent("xmlThing")); then, in a parent class, I can do myXML=preloader.loadedXML and have access to it (preloader is an instance where the LoaderMax stuff happens).

 

If I wanted to be able to access, say, duration and videoTime from my parent class instance, what would I assign? I tried loadedVideo=LoaderMax.getContent("vid"); but that returns a sprite. (I renamed the video instance in the XML file to "vid").

 

I'm sure I'm overlooking something simple here, but it escapes me! Thanks for your help (again)!

Link to post
Share on other sites
If I wanted to be able to access, say, duration and videoTime from my parent class instance, what would I assign? I tried loadedVideo=LoaderMax.getContent("vid"); but that returns a sprite. (I renamed the video instance in the XML file to "vid").

Right, the "content" refers to the ContentDisplay Sprite that is used to display the video. It sounds like what you want is the loader itself because VideoLoader not only can give you info about the video, but it also has controls for playback, volume, etc. In fact, you can build a whole video player on top of VideoLoader. See this tutorial for an example:

 

http://active.tutsplus.com/tutorials/ac ... e-premium/

 

So you could get the loader like this:

 

var loader:VideoLoader = LoaderMax.getLoader("vid");

 

And then get the duration (after the INIT event gets dispatched):

 

loader.duration;

 

And for the record, all ContentDisplay instances have a "loader" property which points to...um...the loader that loaded its rawContent. So technically, you could do this if you prefer:

 

var duration:Number = LoaderMax.getContent("vid").loader;

 

Lots of options :)

Link to post
Share on other sites

Thanks again: I'd initially tried loadedVideo=LoaderMax.getContent("vid") as VideoLoader, but when that returned null, I thought I was doing something wrong.

 

What I think is happening is that onComplete handler fires before the video has actually been loaded, and I'm not sure how (or if I need to) set up an onInit listener for a video loaded through XML. I looked (briefly) at the tutorial you linked, but I didn't see anything with regards to onInit (or how to wrangle a solitary video [not part of a node]).

 

So I've tried moving things out of the onComplete handler...except I'm not sure where I should put them! :) I mean, if I'm trying to load a VideoLoader through XML, is the onComplete handler (for the XML) where I'd start trying to use it?

 

I typed out a bunch of stuff, but I can't see how it would have worked. I'm sure I'm overlooking something simple, and it's driving me batty!

 

All I'm trying to do is load an XML file (which I've done); then load a video (referenced in the XML as a node) and wait until I have access to its properties/metadata before firing the main document's initialization function.

 

I'm sorry to keep asking, but what am I overlooking now? :( Again, thanks!

Link to post
Share on other sites
Thanks again: I'd initially tried loadedVideo=LoaderMax.getContent("vid") as VideoLoader, but when that returned null, I thought I was doing something wrong.

getContent() returns content, not the loader instance. If you want the VideoLoader instance, you use getLoader(), not getContent(). Remember, the loader and the content are distinct. The VideoLoader instance is in charge of loading the raw content and placing it into the ContentDisplay object. The ContentDisplay is simply in charge of displaying the content (hence the name) :).

 

When you cast the content "as VideoLoader", it returned null because a ContentDisplay is not a VideoLoader. The same will happen if you try casting a Sprite to a String or a MovieClip to a TextField (or whatever).

 

What I think is happening is that onComplete handler fires before the video has actually been loaded, and I'm not sure how (or if I need to) set up an onInit listener for a video loaded through XML.

No, I'm 99.999999% sure that's not the case. It isn't firing the onComplete before the video loads.

 

Unless it's important that your ENTIRE video load before the XMLLoader completes, I'd recommend either NOT setting load="true" on your node or set bufferMode="true" so that it allows the XMLLoader to complete as soon as the VideoLoader is buffered. That way, your users don't have to wait so long.

 

Here's sample code:

 

LoaderMax.activate([VideoLoader]);

var video:VideoLoader; //we'll populate this once the XML loads.
var xmlLoader:XMLLoader = new XMLLoader("data.xml", {onComplete:xmlCompleteHandler});
xmlLoader.load();
function xmlCompleteHandler(event:LoaderEvent):void {
   video = LoaderMax.getLoader("vid");
   addChild(video.content);
   video.addEventListener(LoaderEvent.INIT, videoInitHandler);
   video.addEventListener(VideoLoader.VIDEO_BUFFER_FULL, videoBufferFullHandler);
   video.addEventListener(LoaderEvent.COMPLETE, videoLoadedHandler);
   video.load();
   trace("XML done! " + xmlLoader.content);
}

function videoInitHandler(event:LoaderEvent):void {
   trace("Video initted. Duration: " + video.duration);
}

function videoBufferFullHandler(event:LoaderEvent):void {
   trace("video buffer full");
   video.playVideo();
}

function videoLoadedHandler(event:LoaderEvent):void {
   trace("video fully loaded");
}

Link to post
Share on other sites

Jack, thank you! What I was doing wrong was a) setting load to true ;-) and B) failing to properly handle events specific to the VideoLoader instance. Having done that, everything loads and behaves almost like it should!

 

/* IGNORE

The "almost" is a cuepoint issue. The F4V has an embedded cuepoint; in my old, pre-LoaderMax way, I'd just write an onCuePoint handler and assign a metadata object's onCuePoint function to it:

 

var meta:Object=new Object();
meta.onMetaData=onMetaData;
meta.onCuePoint=onCuePoint;
...
function onCuePoint (obj:Object):void
{
target.handleCuePoint(obj);
}

 

This would send the entirety of the cuepoint back to my target (document class) where I'd respond to it. I saw that VideoLoader has a VIDEO_CUE_POINT constant, but I'm not sure what to attach it to? Or more accurately, I'm not sure what I should be listening for in my handleCuePoint handler. Tracing it out, I can see that it's triggered, but it only returns an Event of type "videoCuePoint" Is there some way to retrieve the

*/

 

Okay, figured it out! LoaderEvent.data.name returns the cuePoint's name, which is all I need at this point! I'm guessing there are other LoaderEvent.data.* properties if I need them down the road!

 

Again, thanks for all your patience and help! I really appreciate it!

Link to post
Share on other sites
Okay, figured it out! LoaderEvent.data.name returns the cuePoint's name, which is all I need at this point! I'm guessing there are other LoaderEvent.data.* properties if I need them down the road!

Glad you figured it out. And yes, the "data" of the LoaderEvent contains the entire cue point object that you were used to getting in your old method. So feel free to get whatever data you want on it, just like you're used to.

 

Hopefully you'll find VideoLoader more robust than your old way of doing things - it implements a bunch of workarounds to solve bugs and inconsistencies in the NetStream class (as listed at http://www.greensock.com/loadermax/#bugs). Plus VideoLoader is intended to be much easier to use. Once you get the hang of it, I think you'll see that it's actually less confusing than NetStream/NetConnection and far more capable. :)

 

Enjoy!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×