使用AS3 YouTube播放器时出现内存泄漏。所有侦听器均已删除。请帮助找到解决方案。

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 ------------------------------------------


}

最佳答案

点击监听器又添加到舞台监听器呢?后者未在点击处理程序中注册-错误?

另外,您可能要在注册事件时使用以下命令(弱监听器)

addEventListener(事件类型,处理程序,useCapture,优先级,true);

例如

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


最好的祝福

关于actionscript-3 - YouTube AS3 API内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12384963/

10-13 04:09