我正在编写一款DHTML应用程序,创建一个系统的交互式模拟。该模拟的数据来自另一个工具,并且已经有大量的遗留数据。
模拟中的某些步骤需要播放音频“配音”片段。但我无法找到一种易于跨多个浏览器实现的方法。
Soundmanager2 已经非常接近我所需的功能,但它只能播放mp3文件,而遗留数据可能也包含一些.wav文件。
是否有其他库可以帮助解决问题?
我正在编写一款DHTML应用程序,创建一个系统的交互式模拟。该模拟的数据来自另一个工具,并且已经有大量的遗留数据。
模拟中的某些步骤需要播放音频“配音”片段。但我无法找到一种易于跨多个浏览器实现的方法。
Soundmanager2 已经非常接近我所需的功能,但它只能播放mp3文件,而遗留数据可能也包含一些.wav文件。
是否有其他库可以帮助解决问题?
您将需要包含像Real Audio或QuickTime这样的插件来处理.wav文件,但这应该可以正常工作...
//======================================================================
var soundEmbed = null;
//======================================================================
function soundPlay(which)
{
if (!soundEmbed)
{
soundEmbed = document.createElement("embed");
soundEmbed.setAttribute("src", "/snd/"+which+".wav");
soundEmbed.setAttribute("hidden", true);
soundEmbed.setAttribute("autostart", true);
}
else
{
document.body.removeChild(soundEmbed);
soundEmbed.removed = true;
soundEmbed = null;
soundEmbed = document.createElement("embed");
soundEmbed.setAttribute("src", "/snd/"+which+".wav");
soundEmbed.setAttribute("hidden", true);
soundEmbed.setAttribute("autostart", true);
}
soundEmbed.removed = false;
document.body.appendChild(soundEmbed);
}
//======================================================================
dacracots的代码是干净的基本dom,但也许是没有经过深思熟虑而编写的? 当然,您需要先检查早期嵌入的存在,并保存重复的嵌入创建行。
var soundEmbed = null;
//=====================================================================
function soundPlay(which)
{
if (soundEmbed)
document.body.removeChild(soundEmbed);
soundEmbed = document.createElement("embed");
soundEmbed.setAttribute("src", "/snd/"+which+".wav");
soundEmbed.setAttribute("hidden", true);
soundEmbed.setAttribute("autostart", true);
document.body.appendChild(soundEmbed);
}
在寻找类似情况的解决方案时,我偶然发现了这里的想法。不幸的是,我的浏览器Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.15) Gecko/2009102814 Ubuntu/8.04 (hardy) Firefox/3.0.15在尝试这个时崩溃了。
安装最新更新后,Firefox仍然会崩溃,而Opera则保持正常。
我喜欢dacracot的答案...这里是jQuery中类似的解决方案:
function Play(sound) {
$("#sound_").remove()
$('body').append('<embed id="sound_" autostart="true" hidden="true" src="/static/sound/' + sound + '.wav" />');
}
<audio>
标签。<audio>
标签在IE8-、Android 2.2-、iOS Safari 3.2-或Opera Mini中无法工作:http://caniuse.com/#feat=audio - Jasdeep Khalsa<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
相比于使用插件,有一种更简单的解决方案。IE有自己的bgsound属性,并且还有另一种适用于其他所有浏览器的方法。在这里查看我的教程 = http://www.andy-howard.com/how-to-play-sounds-cross-browser-including-ie/index.html
对于具有wav文件的跨浏览器解决方案,我建议将<audio>
标签设置为默认,并使用@dacracot之前在here提出的<embed>
解决方案,如果用户使用IE,则可以在此处轻松搜索进行检查。