访问ExternalInterface回调中的类变量时出错

时间:2011-08-28 21:26:31

标签: javascript actionscript-3 externalinterface

这是我的动作脚本(用mxmlc编译,嵌入到html中,函数用js调用):

package {
    import flash.display.Sprite;
    import flash.external.ExternalInterface;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class LyrePlayer extends Sprite {
        private var out:SoundChannel;
        private var player:Sound;

        public function LyrePlayer() {
            out = new SoundChannel();
            player = new Sound();
            ExternalInterface.addCallback("play", play);
            ExternalInterface.addCallback("stop", stop);
        }

        private function play(url:String):void {
            var request:URLRequest = new URLRequest(url);
            player.load(request);
            if(out.position != 0) out.stop();
            out = player.play();
        }

        private function stop():void {
            out.stop();
        }
    }
}

这一切都有效。我可以播放一个文件,并且可以多次调用stop()。但是,如果我第二次调用play(),则会抛出错误:

> flashObject.play("/static/test.mp3")
[the song plays]
> flashObject.stop()
[the song stops]
> flashObject.play("/static/test.mp3")
Error
arguments: undefined
message: "Error calling method on NPObject."
> flashObject.stop()
[no error]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好像你需要拨打player.close(),这里是working example