2012-09-12 115 views
3

使用AS3 youtube播放器時出現內存泄漏。所有聽衆都被刪除。請幫助找到解決辦法。Youtube AS3 API內存泄漏

public class YoutubeStreamTest extends Sprite 
{ 
    private var _loader : Loader; 
    private var _player : Object; 
    private var _spr : Sprite; 

    // constructor ----------------------------------------- 
    public function YoutubeStreamTest() 
    { 
     addEventListener(Event.ADDED_TO_STAGE, hAddedToStage); 
    } 

    // methods --------------------------------------------- 

開始加載視頻

private function startVideo() : void { 
     _loader = new Loader(); 
     _loader.contentLoaderInfo.addEventListener(Event.INIT, hLoaderInit, false, 0, true); 
     _loader.load(new URLRequest("http://www.youtube.com/v/FooZ5x-OX0E?version=3&controls=0&showinfo=0&end=3")); //yPrHf776eWA = the vid u want t play 
    } 

    // handlers -------------------------------------------- 

等待視頻準備

private function hLoaderInit(event : Event) : void { 
     _loader.contentLoaderInfo.removeEventListener(Event.INIT, hLoaderInit); 
     _player = _loader.content; 
     _player.addEventListener("onReady", hPlayerReady, false, 0, true); 
     addChild(_player as DisplayObject); 
     _player.x = 0; 
     _player.y = 20; 

     _spr = new Sprite(); 
     _spr.graphics.beginFill(0x0, .1); 
     _spr.graphics.drawRect(0, 0, 500, 500); 
     _spr.graphics.endFill(); 
     addChild(_spr); 
    } 

在準備 - 開始通過點擊

private function hPlayerReady(event : Event) : void 
    { 
     _player.removeEventListener("onReady", hPlayerReady); 
     _player.setSize(440, 260); 
     _player.playVideo(); 
    } 

    private function hAddedToStage(event : Event) : void { 
     Security.allowInsecureDomain("*"); 
     Security.allowDomain("*"); 
     startVideo(); 

     addEventListener(MouseEvent.CLICK, hClick); 
    } 

加載新的視頻實例

private function hClick(event : Event) : void { 
     removeEventListener(Event.ADDED_TO_STAGE, hAddedToStage); 
     if(_player) { 
      removeChild(_player as DisplayObject); 
      _player.destroy(); 
      removeChild(_spr); 
      _loader.unloadAndStop(true); 
     } 
     startVideo(); 
    } 

    // getsetters ------------------------------------------ 


} 

回答

0

那麼點擊監聽器,並添加到舞臺監聽器?後者在點擊處理程序中未註冊 - 錯誤?

你也可能需要使用以下時註冊事件(弱聽衆)

的addEventListener(事件類型,處理的方法,useCapture,優先級,真正);

例如

addEventListener(MouseEvent.CLICK, onClicked, false, 0, true); 

問候