开发者

Flash sound not playing

开发者 https://www.devze.com 2023-02-08 23:45 出处:网络
I have a code as below import flash.media.Sound; import flash.media.SoundChannel; flash.system.Security.allowDomai开发者_运维百科n(\"*\");

I have a code as below

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

flash.system.Security.allowDomai开发者_运维百科n("*");

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

The problem is that when I try to play this beep using JS, I get the following error:

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

If I directly add "playBeep()" in the AS3 code, then it plays the sound. Any clue why? I have added allowscriptaccess=always in the embed code.


Is it possible that you're calling the beep function from your Javascript before the beep.mp3 has completely loaded in the swf? You can get an error by calling .play() on a Sound object that is not ready.

Try either wrapping the call to .play() in a try/catch block, or perhaps put the ExternalInterface.addCallback declaration inside of an onLoadComplete handler for the sound object.

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

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

EDIT: Of course, this doesn't account for the next question - How does the Javascript know that the mp3 has loaded? In that case, the onLoadComplete handler could also call

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

In which notifyJSThatMP3isHere is a function you've defined in your Javascript that lets it know that it's now okay to try to call the swf's exposed "beep" function.

0

精彩评论

暂无评论...
验证码 换一张
取 消