在html中预加载我的音频文件

Preloading my audio file in html

本文关键字:我的 音频 文件 加载 html      更新时间:2023-09-26

我目前正在使用下面的方式在我的网站上播放声音效果。我需要每次从从设备接收到协议时播放声音:

<script>
function RxProtocol()
{
    playSound('audio123.wav');
}
function playSound(soundfile) 
{
    document.getElementById("dummy").innerHTML=
    "<embed src='""+soundfile+"'" hidden='"true'" autostart='"true'" loop='"false'" />";
}
</script>
<body>
<span id="dummy"></span>
</body>

但似乎这样播放的音频每次都延迟。在我收到协议2秒后,音频才开始播放。

是因为我没有预加载我的音频吗?你们能教我如何修改我的代码并使其成为预加载启用吗?

您可以尝试html5 audio tag中的preload attribute

like preload="auto"

也可阅读http://webdesignledger.com/tips/audio-and-video-with-html5和http://code.google.com/p/html5-preloader/

不知怎么的,我下面的代码适用于firefox和chrome,但不适用IE9:

<script>
function RxProtocol()
 {
    var a = document.getElementById("audio1");
    a.play();
 }
</script>
<body>
<audio id="audio1">
<source src="audio.wav" type="audio/wav">
<source src="audio.mp3" type="audio/mpeg">
audio tag not supported.
</audio>
</body>

试了又试,我发现我需要把下面的IE9工作。Firefox和Chrome也可以:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

在此之前,我把下面的代码只在Firefox和Chrome上工作:

 <meta http-equiv="Pragma" content="no-cache"/>

你们知道这两个meta有什么区别吗?