Embedd .wav files in HTML page in all browsers (no

2019-04-08 15:16发布

I need to play few wav files on button click. I found solution working in IE but it requires QickTime plugin for Firefox.

Is there any other way?

<html>
    <head>
        <script>
function DHTMLSound(surl) {
  document.getElementById("dummyspan").innerHTML=
    "<embed src='"+surl+"' hidden=true autostart=true loop=false>";
}
        </script>
    </head>
    <body>
        <h1>test</h1>
        <span id=dummyspan></span>
        <input type="button" value="Play" onmouseover="DHTMLSound('1.wav')">
    </body>
</html>

3条回答
闹够了就滚
2楼-- · 2019-04-08 15:55

Use one of these. I have only use jPlayer and can strongly recommend it.

jPlayer (requires Flash)

Scriptaculous plugin (works without Flash in Firefox)

MooTools (requires Flash)

查看更多
Fickle 薄情
3楼-- · 2019-04-08 16:03

I'd detect whether the browser allows the audio tag, and use that in that case.

That looks like this:

<audio src="1.wav" autoplay></audio>

Currently Firefox, Safari and Opera can play Wavs, Chrome as of version 3 can't, not sure about 4.

See http://html5doctor.com/native-audio-in-the-browser/ for info on how to detect whether the browser has the audio tag.

You'd then use your existing solution for IE.

查看更多
Lonely孤独者°
4楼-- · 2019-04-08 16:15

<audio> as per Rich's answer is definitely the way of the future. Unfortunately at the moment there is no IE support and to get the other browsers that support it to be happy you have to use both WAV and (OGG or MP3).

So for the moment you may need to provide other ways instead or as-well-as <audio>.

Personally I would strongly avoid <embed>ding a media player plugin. It won't work on browsers without plugins and you might not get a plugin you expect, and the one you get might not work the same as you expect. There is also <bgsound> on IE only, but controlling it can be annoying.

So I'd probably go for a Flash fallback solution for when <audio> isn't available. Flash has much better acceptance than any of the media player plugins.

Unfortunately, it doesn't natively support WAV, so either you use a (typically slow) WAV reader or you go with MP3 and have multiple audio formats to worry about again!

One day this will all work nicely. One day, probably around 2056.

查看更多
登录 后发表回答