為你的html5網(wǎng)頁添加音效示例
來源:易賢網(wǎng) 閱讀:1628 次 日期:2016-07-11 10:12:52
溫馨提示:易賢網(wǎng)小編為您整理了“為你的html5網(wǎng)頁添加音效示例”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了為你的html5網(wǎng)頁添加音效示例,需要的朋友可以參考下

為交互添加恰當?shù)囊粜?,常常能改善用戶體驗。在我們所熟悉的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        //是否循環(huán)播放,這個參數(shù)現(xiàn)在沒有用上

};

if(profile) {

$.extend(this.profile,profile);

}

this.soundObj=null;

this.isIE = !-[1,];

/*這個方法是前輩大牛發(fā)明的,利用ie跟非ie中JScript處理數(shù)組最后一個逗號“,”的差異,

不過對于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是音量的最小值。先把音量關(guān)到最小,免得一加載就叮的一聲,嚇到人。

}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!='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>

更多信息請查看網(wǎng)頁制作
易賢網(wǎng)手機網(wǎng)站地址:為你的html5網(wǎng)頁添加音效示例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇剩?/div>
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)