1

我正在尝试让 youtube as3 无铬播放器工作。我遵循了youtube as3 API 示例,这就是我到目前为止所得到的:

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 事件触发,但我得到错误。跟踪 Object(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.

4

1 回答 1

1

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

包裹
{
 导入 flash.display.*;
 导入 flash.events.*;
 导入 flash.system.Security;
 导入 flash.net.*;

 公共类 Main 扩展了 MovieClip
 {
  Security.allowDomain("*");

  私有变量播放器:对象;
  私有变量加载器:加载器;

  公共函数 Main():void
  {
   如果(阶段)初始化();
   否则 addEventListener(Event.ADDED_TO_STAGE, init);
  }

  私有函数 init(e:Event = null):void
  {
   removeEventListener(Event.ADDED_TO_STAGE, init);

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

  私有函数 onLoaderInit(e:Event):void
  {
   播放器 = 雪碧(loader.content);
   addChild(加载器);
   player.addEventListener("onReady", onPlayerReady);
   player.addEventListener("onError", onPlayerError);
   player.addEventListener("onStateChange", onPlayerStateChange);
   player.addEventListener("onPlayerQualityChange", onVideoPlaybackQualityChange);
  }

  私有函数 onPlayerReady(e:Event):void
  {
   trace("玩家准备好:" + Object(e.target).Data);
   player.loadVideoById("uad17d5hR5s");
   player.setSize(480, 365);
  }
  私有函数 onPlayerError(e:Event):void
  {
   trace("播放器错误:" + Object(e).Data);
  }
  私有函数 onPlayerStateChange(e:Event):void
  {
   // trace("玩家状态:" + Object(e).Data);
  }
  私有函数 onVideoPlaybackQualityChange(e:Event):void
  {
   trace("视频质量:" + Object(e).Data);
  }
 }
}
于 2009-10-25T01:44:24.747 回答