AS3声音加载

时间:2012-11-12 20:44:13

标签: actionscript-3

我使用下面的代码加载外部mp3文件:

//Create an instance of the Sound class
var soundClip:Sound=new Sound();

//Create a new SoundChannel Object
var sndChannel:SoundChannel=new SoundChannel();

//Load sound using URLRequest
soundClip.load(new URLRequest("namesounds/agamemnonas.mp3"));

//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE,onComplete,false,0,true);
function onComplete(evt:Event):void {
    //Play loaded sound
    sndChannel=soundClip.play();
}

同样在我的html中,我有一个下拉列表:

<select name="sounds" id="sounds">
  <option value="sounds/eden.mp3" selected="selected">eden</option>
  <option value="sounds/zeus.mp3">zeus</option>
  <option value="sounds/earth.mp3">earth</option>
</select>

是否可以从下拉菜单发送路径到Flash文件? 例如:

//Load sound using URLRequest
soundClip.load(new URLRequest("namesounds/agamemnonas.mp3"));

URLRequest必须是我的.php文件下拉菜单列表中的路径。

1 个答案:

答案 0 :(得分:0)

您必须在AS中使用ExternalInterface.addCallback并从javascript调用该函数

AS3

ExternalInterface.addCallback("loadSoundClip", loadSoundClip);
function loadSoundClip(mp3File:String):void{
    soundClip.load(new URLRequest(mp3File));
}

纯Javascript

// This is the <select>
var sounds = document.getElementById('sounds');
// This is the <object>/<embed>
var flashObject = document.getElementById('flashObject');
sounds.onchange = function(){
    flashObject.loadSoundClip(sounds.options[sounds.selectedIndex].value);
};

或者

的jQuery

// This is the <select>
var sounds = $('#sounds');
// This is the <object>/<embed>
var flashObject = $('#flashObject');
sounds.on('change', function(){
    flashObject[0].loadSoundClip($(this).val());
});

不要忘记将allowscriptaccess / object的{​​{1}}设置为正确的值,以便页面可以与Flash对象进行通信