views:

519

answers:

3

I'm goofing around with the new youtube as3 API but got stuck. This is how far i got (from looking at their sample code).

http://pastie.org/656088

public class Main extends Sprite 
{
    Security.allowDomain("*");

    private var player:Object;
    private var loader:Loader;

    public function Main():void 
    {
     if (stage) init();
     else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
     removeEventListener(Event.ADDED_TO_STAGE, init);

     loader = new Loader();
     loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
     loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
    }

    private function onLoaderInit(e:Event):void
    {
     addChild(loader);
     loader.contentLoaderInfo.addEventListener("onReady", onPlayerReady);
     loader.contentLoaderInfo.addEventListener("onError", onPlayerError);
     loader.contentLoaderInfo.addEventListener("onStateChange", onPlayerStateChange);
     loader.contentLoaderInfo.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
    }

    private function onPlayerReady(e:Event):void
    {
     trace("Player ready: " + Object(e).Data);
    }
    private function onPlayerError(e:Event):void
    {
     trace("Player error: " + Object(e).Data);
    }
    private function onPlayerStateChange(e:Event):void
    {
     trace("Player state: " + Object(e).Data);
    }
    private function onVideoPlaybackQualityChange(e:Event):void
    {
     trace("Video quality: " + Object(e).Data);
    }
}

I don't really know what the next step is. I get no errors and nothing gets traced. I'm pretty sure that my events are not implemented correctly.

Update: I followed Amarghosh's answered and did this instead:

private function onLoaderInit(e:Event):void
{
  player = Sprite(loader.content);
  addChild(player);
  player.addEventListener("onReady", onPlayerReady);
  player.addEventListener("onError", onPlayerError);
  player.addEventListener("onStateChange", onPlayerStateChange);
  player.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
}

Now the onPlayerReady and the onStateChange events fires but i get errors. When tracing Object(e).Data i get this error

ReferenceError: Error #1069: the property Data was not found for com.google.youtube.event.ExternalEvent and there is no standard value. (stranslated from swedish) When changing to Object(e.target).Data it traces "undefined" and Object(e.target) traces [object SwfProxy].

If i try player.loadVideoById("uad17d5hR5s"); i get this error:

1061: Call to a possibly undefined method loadVideoById through a reference with static type flash.display:Sprite.

A: 

If the variable player is supposed to hold the loaded youtube player, change its type from Object to something more solid like Sprite.

private var player:Sprite;

Now, I don't know the youtube API, but there is a problem in your onLoaderInit method. You should be adding those listeners to the loaded content instead of its LoaderInfo object. LoaderInfo dispatches events related to the loading process - it's not relevant once the loading is successful. The events mentioned in those calls would be dispatched by the loaded content. Change the method to:

private function onLoaderInit(e:Event):void
{
  player = Sprite(loader.content);
  addChild(player);
  player.addEventListener("onReady", onPlayerReady);
  player.addEventListener("onError", onPlayerError);
  player.addEventListener("onStateChange", onPlayerStateChange);
  player.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
}
Amarghosh
Great! One step closer. I have updated my post. Thank you very much.
Johan
It's a good idea to ask a new question once your problem (that you asked in the question) is solved, rather than continuing to discuss all issues in the same thread.
Amarghosh
It seems that the problem is related to youtubeapi - ask it as another question to get more exposure.
Amarghosh
Allright, will do that!
Johan
A: 

Hi, relatively recently youtube released a wrapper library for their chromeless player. You should definitly use this version (you could write it again yourself, but thats reinventing the wheel), check out the tutorial/example on this page

ActionScript 3.0 Wrapper for Chromeless Player

That wrapper was released a quite a while ago and not by youtube. The wrapper is for the as2 API and i want to try out the as3 API.
Johan
I'm sure this is true, but the link above is for the AS3 wrapper library, the library that is for ActionScript 3.0, strictly typed events and everything.
And I'm sure the wrapper has got strictly typed events and everything but it is still using the old deprecated API.
Johan
I'm sorry Johan you were right, after comparing the url's of the chromeless swf I see http://www.youtube.com/apiplayer?enablejsapi=1 for the wrapper library and http://www.youtube.com/apiplayer?version=3 for the new one. Many apologies, I think I may have solved your other problem though....
A: 

Hi Johan, sorry for all the library confusion, I think I have the answer to your other error though. When you do this Sprite(loader.content) you 'force' cast the player to be a sprite, because you want methods of the api I would recommend using a plain old object, because it won't complain about untyped methods:

// No particluar type
var player:Object;

private function onLoaderInit(e:Event):void
{
 player = loader.content;
 addChild(player as DisplayObject);

 var dispatcher:IEventDispatcher = player as IEventDispatcher;
 dispatcher.addEventListener("onReady", onPlayerReady);
 dispatcher.addEventListener("onError", onPlayerError);
 dispatcher.addEventListener("onStateChange", onPlayerStateChange);
 dispatcher.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
}
That was it! Thanks!
Johan