用HTML5为你的网页添加音效

guohaibolichao

guohaibolichao

2016-02-19 18:18

下面请跟着图老师小编一起来了解下用HTML5为你的网页添加音效,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!

为交互添加恰当的音效,常常能改善用户体验。在我们所熟悉的windows里,清空回收站的碎纸声就是很好的例子。

下面是一个利用HTML5, Jquery,给页面添加音效的小组件(只是添加音效,并不是播放器)。

其实很简单,就是利用HTML5中的audio标签,播放声音。不过为了照顾IE 6-8,还是用上了bgsound。

兼容所有主流浏览器(非主流不在考虑之列了)

闲话少说,上代码:

a href="#" class="fui-btn"播放/a

script
/*Play sound component*/
/*
* profile: 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.init();
};
FUI.soundComponent.prototype={
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" autobuffersource src="'+this.profile.src+'" /source src="'+this.profile.altSrc+'" //audio').appendTo('body');
}
}
},
setSrc:function(src,altSrc){
this.profile.src=src;
if(typeof altSrc!='undefined'){
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.mp3'});
$('.fui-btn').bind('click',function(e){
sd.play();
});
/script

附表

FormatIE 9Firefox 3.5Opera 10.5Chrome 3.0Safari 3.0 Ogg VorbisNoYesYesYesNo MP3YesNoNoYesYes WavNoYesYesYesYes

Format 

IE 9 

Firefox 3.5 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

Opera 10.5 

Chrome 3.0 

Safari 3.0 

Ogg Vorbis 

No 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

Yes 

Yes 

Yes 

No 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

MP3 

Yes 

No 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

No 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

Yes 

Yes 

Wav 

No 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

Yes 

Yes 

Yes

Yes

展开更多 50%)
分享

猜你喜欢

用HTML5为你的网页添加音效

Web开发
用HTML5为你的网页添加音效

试着用HTML5制作网页的过程

平面设计 海报设计 广告设计 画报设计 签名设计 服装设计 名片设计 画册设计 版式设计 商标设计
试着用HTML5制作网页的过程

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

html5拖曳操作 HTML5实现网页元素的拖放操作

Web开发
html5拖曳操作 HTML5实现网页元素的拖放操作

html5 标签

Web开发
html5 标签

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

隔岸观火 为你解读Flash和HTML5那点事

Web开发
隔岸观火 为你解读Flash和HTML5那点事

HTML5 File API改善网页上传功能

Web开发
HTML5 File API改善网页上传功能

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

WPS文字2018中怎么给文字添加删除线

WPS文字2018中怎么给文字添加删除线

CSS3教程:由CSS3代码生成的图形与图标设计

CSS3教程:由CSS3代码生成的图形与图标设计
下拉加载更多内容 ↓