为交互添加恰当的 音效 ,常常能改善用户体验。在我们所熟悉的windows里, 清空回收站 的碎纸声就是很好的例子。
下面是一个 利用 HT ML 5, jq uery,给页面添加音效的小组件(只是添加音效,并不是播放器)。
其实很 简单 ,就是利用HTML5中的audio标签,播放 声音 。 不过 为了照顾IE 6-8,还是用上了bgsound。
兼容所有主流浏览器(非主流不在考虑之列了)
闲话少说,上代码:
复制代码
代码如下:
<a hr ef=" # " class="fui- BT n">播放</a>
<script>
/* play sound @R_ 360 _480@*/
/*
* PR ofile: JSON, {src:'chimes.wav',altSrc:'',loop:false}
*
* setSrc: Function, set the source of sound
* play: Function, play sound
*/
if (!FUI){
VAR FUI = {};
}
FUI.soundComponent=function(profile){
this.profile={
src:'', //音频文件地址
altSrc:'', //备选音频文件地址 (不同浏览器支持的 音频格式 不同,可见附表)
loop:false // 是否 循环播放,这个参数现在没有用上
};
if(profile) {
$.extend(this.profile,profile);
}
this.soundObj=null;
this.isIE = !-[1,];
/*这个方法是前辈大牛发明的,利用ie跟非ie中JScript处理数组最后一个逗号[,]的差异,
不过对于IE 9,这个办法就无效了,但此处正合我用,因为IE 9支持audio*/
this.in IT ();
};
FUI.soundComponent.prototy PE ={
init:function(){
this._setSrc();
},
_setSrc:function(){
if(this.soundObj){
if(this.isIE){
this.soundObj[0].src=this.profile.src;
}else{
this.soundObj[0].innerHTML='<source src="'+this.profile.src+'" />
<source src="'+this.profile.altSrc+'" />';
}
}else{
if(this.isIE){
this.soundObj=$
('<bgsound volume="-10000" loop="1" src="'+this.profile.src+'">').appendTo('body');
//-10000是 音量 的 最小值 。先把音量关到最小,免得一加载就叮的一声,吓到人。
}else{
this.soundObj=$('<audio preload="auto" autobuffer>
<source src="'+this.profile.src+'" />
<source src="'+this.profile.altSrc+'" />
</audio>').appendTo('body');
}
}
},
setSrc:function(src,altSrc){
this.profile.src=src;
if(typeof altSrc!='un define d'){
this.profile.altSrc=altSrc;
}
this._setSrc();
},
play:function(){
if(this.soundObj){
if(this.isIE){
this.soundObj[0].volume = 1; //把音量打开。
this.soundObj[0].src = this.profile.src;
}else{
this.soundObj[0].play();
}
}
}
};
var sd=new FUI.soundComponent({src:'ding.wav',altSrc:'ding.m P3 '});
$('.fui-btn').bind('click',function(e){
sd.play();
});
</script>
总结
以上是 为你收集整理的 为你的html5网页添加音效示例 全部内容,希望文章能够帮你解决 为你的html5网页添加音效示例 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
查看更多关于为你的html5网页添加音效示例的详细内容...