闪烁的声音没有播放

时间:2011-02-08 06:31:10

标签: flash actionscript-3

我的代码如下

import flash.media.Sound;
import flash.media.SoundChannel;

flash.system.Security.allowDomain("*");

var request:URLRequest = new URLRequest('beep.mp3');
var beepSound:Sound = new Sound();
beepSound.load(request);

function playBeep() {
    var channel:SoundChannel = beepSound.play();
}

ExternalInterface.addCallback("beep", playBeep);

问题是,当我尝试使用JS播放此哔声时,我收到以下错误:

Error calling method on NPObject! [plugin exception: Error in Actionscript. Use a try/catch block to find error.].

如果我在AS3代码中直接添加“playBeep()”,那么它会播放声音。有什么线索的原因?我在embed代码中添加了allowscriptaccess =。

1 个答案:

答案 0 :(得分:1)

你是否有可能在beep.mp3完全加载到swf之前从你的Javascript调用beep函数?您可以通过在尚未就绪的Sound对象上调用.play()来获取错误。

尝试在try / catch块中包装对.play()的调用,或者将ExternalInterface.addCallback声明放在声音对象的onLoadComplete处理程序中。

beepSound.addEventListener(Event.COMPLETE, onLoadComplete);
beepSound.load(request);

function onLoadComplete(e:Event):void{
    ExternalInterface.addCallback("beep", playBeep);
    beepSound.removeEventListener(Event.COMPLETE, onLoadComplete);
}

编辑:当然,这不能解释下一个问题 - Javascript如何知道mp3已加载?在这种情况下,onLoadComplete处理程序也可以调用

ExternalInterface.call("notifyJSThatMP3isHere", "someArgs");

其中notifyJSThatMP3isHere是你在Javascript中定义的一个函数,它让它知道现在可以尝试调用swf暴露的“beep”函数。