2009-10-23 94 views
1

我試圖讓YouTube的as3 chromeless播放器工作。我按照youtube as3 API examples,這是我得到迄今: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事件觸發,但我得到的錯誤。當跟蹤對象(e)中。數據我得到這個錯誤:

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

當改變到Object(e.target).Data它跡線「未定義」和Object(e.target)痕跡「[對象SwfProxy]」。

如果我嘗試player.loadVideoById("uad17d5hR5s");我得到這個錯誤:

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

+0

這是http://stackoverflow.com/questions/1608027/youtube-chromeless-as3-player – 2009-10-23 13:08:56

+0

的後續問題關於'1061'錯誤MovieClip類,您可以通過將'player'的類型更改爲MovieClip而不是Sprite來消除此錯誤。否則,你將不得不將它輸回到對象 - 我知道我讓你改變它:( – Amarghosh 2009-10-24 07:42:21

回答

1

我不認爲你應該投loader.content的雪碧。您應該將播放器轉換爲對象類型。 player變量只能訪問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); 
    } 
} 
} 
相關問題