[英]Flash sound not playing
我有如下代碼
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()”,則它將播放聲音。 有什么線索嗎? 我在嵌入代碼中添加了allowscriptaccess = always。
在beep.mp3完全加載到swf中之前,是否有可能從Javascript調用beep函數? 您可以通過在尚未准備就緒的Sound對象上調用.play()來獲取錯誤。
嘗試將對.play()的調用包裝在try / catch塊中,或者將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的公開“嗶嗶”功能了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.