当前位置: 动力学知识库 > 问答 > 编程问答 >

html5 - Play audio tag with javascript in firefox/internet explore/chrome

问题描述:

http://www.storiesinflight.com/html5/audio.html

I'm trying to accomplish a sound script from above link, which should play a audio file. The article author claim it's compatible in firefox. But it's not playing in mine. It does however works in Chrome. Tried internet explore and not luck there either.

Are there any obvious reasons for this?

 <audio id="audiotag1" src="path.wav" preload="auto"></audio>

<a href="javascript:play_single_sound();">Play 5-sec sound on single channel</a>

<script type="text/javascript">

function play_single_sound() {

document.getElementById('audiotag1').play();

}

</script>

网友答案:

IE 11 does not support .wav files, so if you want to support IE you'll have to use either .mp3 or .ogg. As for why it didn't work in Firefox, a problem might be that .wav files are very large so depending on when you clicked on the link the audio file may not have loaded yet.

It's better to use the canplay event so you know when the audio file has loaded and can be played.

var audio = document.getElementById('audiotag1');
audio.addEventListener('canplay', function() {
  audio.play();
});
网友答案:

Which version of IE are you testing it on?

IE often lags in terms of support for features.

Check out this chart. Maybe your version of IE doesn't have support for audio.


UPDATE

Adding this meta tag might help

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

Installing MediaPack might help too

网友答案:

Were using .wav, tried with .mp3 and it works in all three browsers.

分享给朋友:
您可能感兴趣的文章:
随机阅读: