簡體   English   中英

AS3聲音加載

[英]AS3 sound loading

我正在使用下面的代碼加載外部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文件中下拉菜單列表中的路徑。

您必須在AS中使用ExternalInterface.addCallback並從javascript調用該函數

AS3

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

純Java腳本

// 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());
});

不要忘記將對objectallowscriptaccess設置/ embed到正確的值,以便頁面可以與Flash對象通信

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM