error accessing class variables within an ExternalInterface callback
Here's my actionscript (compiled with mxmlc, embedded into html, and the functions are called with 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(r开发者_如何学编程equest);
if(out.position != 0) out.stop();
out = player.play();
}
private function stop():void {
out.stop();
}
}
}
This all works, sort of. I can play one file, and call stop()
any number of times. But if I call play()
a second time, it throws an error:
> 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]
Any ideas?
seems like you need to call player.close()
, here's a working example
精彩评论