方式一:
/** * 播放音频(Chrome、opera)支持 * @param file:支持 rm,mid,wav */ function playAudio(file) { var embed=document.getElementById("bgsoundid"); if(embed){ document.removeChild(embed); } embed = document.createElement("embed"); embed.setAttribute('id', "bgsoundid"); embed.setAttribute('src', file); embed.setAttribute('hidden', true); embed.setAttribute('autostart', true); embed.setAttribute('loop', "1"); document.body.appendChild(embed); }
方式二:
/** * Iframe实现声音播放(兼容:IE、firefox、chrome、Opera) * @param file 支持 rm,mid,wav,*基本上都支持 */ function iframeAudio(file){ var iframe=document.getElementById("audioIframe"); if(iframe) { document.removeChild(iframe); } iframe = document.createElement("iframe"); iframe.setAttribute('id', "audioIframe"); iframe.setAttribute('src', file); iframe.setAttribute('height', "0"); iframe.setAttribute('width', "0"); iframe.setAttribute('border', "0"); document.body.appendChild(iframe); }
方式三:
<bgsound loop='10' src='../res/audio/alarm.wav'>
方式四:
使用soundManageer2插件,下载地址:http://www.schillmania.com/projects/soundmanager2/#getting-started
var soundManager=soundManager.createSound({ id: 'mySound', url: '/path/to/some.mp3', autoLoad: true, autoPlay: false, onload: function() { alert('The sound '+this.id+' loaded!'); }, volume: 50 }); //播放 soundManager.play(); //停止 soundManager.stop();