Youtube as3播放器API错误

时间:2009-10-23 12:55:08

标签: actionscript-3 youtube youtube-api

我正试图让youtube as3无框播放器工作。我跟着youtube as3 API examples,这是我到目前为止所得到的:

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);
    }
}

onPlayerReady和onStateChange事件触发但我收到错误。跟踪对象(e).Data时出现此错误:

ReferenceError: Error #1069: the property Data was not found for com.google.youtube.event.ExternalEvent and there is no standard value.
(瑞典语翻译)

当更改为Object(e.target).Data时,它会跟踪“未定义”和Object(e.target)跟踪“[object SwfProxy]”。

如果我尝试player.loadVideoById("uad17d5hR5s");,我会收到此错误:

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

1 个答案:

答案 0 :(得分:1)

我认为你不应该将loader.content强制转换为Sprite。您应该将播放器转换为Object类型。播放器变量仅用于访问API调用。在放置,移动和添加到显示列表方面,使用包含SwfProxy对象的Loader对象。试试这段代码:

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);
  }
 }
}