views:

841

answers:

1

I'm trying to get the youtube as3 chromeless player to work. I have followed the youtube as3 API examples and this is what i got so far:

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

    private var player:Sprite;
    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
    {
     player = Sprite(loader.content);
     addChild(player);
     player.addEventListener("onReady", onPlayerReady);
     player.addEventListener("onError", onPlayerError);
     player.addEventListener("onStateChange", onPlayerStateChange);
     player.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
    }

    private function onPlayerReady(e:Event):void
    {
     trace("Player ready: " + Object(e.target).Data);
     // player.loadVideoById("uad17d5hR5s");
    }
    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);
    }
}

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.

+1  A: 

I don't think you should cast loader.content as a Sprite. You should cast the player to an Object type instead. The player variable is only to access the API calls. In terms of placing, moving and adding to the display list, use the Loader object that contains the SwfProxy object. Try this code:

package
{
 import flash.display.*;
 import flash.events.*;
 import flash.system.Security;
 import flash.net.*;

 public class Main extends MovieClip 
 {
  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
  {
   player = Sprite(loader.content);
   addChild(loader);
   player.addEventListener("onReady", onPlayerReady);
   player.addEventListener("onError", onPlayerError);
   player.addEventListener("onStateChange", onPlayerStateChange);
   player.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
  }

  private function onPlayerReady(e:Event):void
  {
   trace("Player ready: " + Object(e.target).Data);
   player.loadVideoById("uad17d5hR5s");
   player.setSize(480, 365);
  }
  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);
  }
 }
}
Robert Abramski