我正在使用新的youtube as3 API,但却陷入困境。这是 我有多远(从查看他们的示例代码)。
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);
}
}
我真的不知道下一步是什么。我没有错误,也没有任何内容被追踪。我很确定我的活动没有正确实施。
更新: 我跟着Amarghosh的回答而做了这件事:
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);
}
现在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时,它会跟踪“undefined”和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.
答案 0 :(得分:0)
如果变量player
应该包含加载的youtube播放器,请将其类型从Object
更改为更加稳固的Sprite
。
private var player:Sprite;
现在,我不知道youtube API,但您的onLoaderInit
方法存在问题。您应该将这些侦听器添加到已加载的内容而不是其LoaderInfo
对象。 LoaderInfo
调度与加载过程相关的事件 - 一旦加载成功,它就无关紧要了。这些调用中提到的事件将由加载的内容分派。将方法更改为:
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);
}
答案 1 :(得分:0)
最近youtube为他们的无边框播放器发布了一个包装库。你应该明确地使用这个版本(你可以自己再写一次,但那可以重新发明轮子),看看这个页面上的教程/例子
答案 2 :(得分:0)
Sprite(loader.content)
时,你强制'将播放器强制转换为精灵,因为你想要api的方法我建议使用普通的旧对象,因为它不会抱怨无类型的方法:
// 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);
}