回答问题:http://www.iteye.com/problems/22309
一般做背景声的做法是用bgsound或embed,但是它们跟浏览器都有关,不如直接用flash来做得方便.
1.FLASH端代码如下:
//SoundSwf.as package { import flash.display.*; import flash.events.*; import flash.media.Sound; import flash.net.URLRequest; import flash.external.ExternalInterface; import flash.utils.*; public class SoundSwf extends Sprite{ private var map:Object = new Object(); public function SoundSwf():void{ if (ExternalInterface.available){ ExternalInterface.addCallback("playSound", this.playSound); } } public function playSound(url:String,count:int):void{ var s:Sound; if(!map.hasOwnProperty(url)){ s = new Sound(); map.url=s; s.addEventListener(Event.COMPLETE,function(e:Event):void{ var localSound:Sound = e.target as Sound; localSound.play(0,count); }); s.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{ExternalInterface.call("alert", e);}); s.load(new URLRequest(url)); }else{ s = map.url as Sound; s.play(0,count); } } } }
2. JS端调用:
<noscript>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="1" height="1" id="SoundSwf" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="SoundSwf.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /> <embed src="SoundSwf.swf" quality="high" bgcolor="#ffffff" width="1" height="1" name="SoundSwf" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_cn" />
</object>
</noscript>
<script>
window.οnlοad=function(){
SoundSwf.playSound('event.mp3',2);
//SoundSwf.playSound('alarm.mp3',1);
//SoundSwf.playSound('event.mp3',4);
}
</script>
详细见附件.
ps: js段建议用SwfObject来载入swf.
另外,该示例中, 在onload事件里面调用是不可靠的,因为有可能这时候swf还没初始化完