兼容FF/IE的告警声播放(基于flash)

回答问题: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还没初始化完

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值