Play HTML audio in Internet Explorer?

2019-07-15 20:13发布

I have this html and js which I'm using to play some audio:

HTML

<div id="audio-clips">
    <audio src="/audio/cha-ching.wav" type="audio/wav" id="audio-cha-ching">
</div>

JavaScript

var sfx = $("#audio-clips #audio-cha-ching")[0];
sfx.play();

This is failing in IE 8, due to it not recognising the "play" method. Is there a jQuery audio-playing method which works cross-browser, with the same code? I'd rather do that then try to fall back to some IE-specific solution (as browser-specific solutions are always brittle).

4条回答
ゆ 、 Hurt°
2楼-- · 2019-07-15 20:56

Encode your audio in mp4 format, instead of wav. This works for me in IE 11.

查看更多
时光不老,我们不散
3楼-- · 2019-07-15 21:09

HTML5 support on IE started on IE9 :(.

By the way, I am not sure if WAV is played by IE(I would not say so): http://en.wikipedia.org/wiki/HTML5_Audio

Fallback solution for HTML and legacy browsers

查看更多
Luminary・发光体
4楼-- · 2019-07-15 21:11

The audio tag is available in Internet Explorer 9 and above. You can check this using Can I Use - Audio.

The wav format is supported by Firefox 3.6+, Safari 5+, Opera 10.5+ and Internet Explorer 9+ on the audio tag. You would need to add an MP3 source in addition to the wav source to get Chrome support.

To get backwards compatible support, you use the following - Flash element omitted for brevity.

<audio controls preload="auto" autobuffer> 
  <source src="sound.mp3" />
  <source src="sound.ogg" />
  <source src="sound.wav" />
  <!-- now include flash fall back -->
</audio>
查看更多
手持菜刀,她持情操
5楼-- · 2019-07-15 21:12

I would strongly recommend using howler.js for playing audio files. It has great compatibility across browsers and devices. It uses HTML5 audio when available, and falls back to older technologies when necessary.

查看更多
登录 后发表回答