音频/视频DOM错误事件在Chrome / Firefox中不起作用

时间:2018-06-22 16:13:08

标签: javascript html html5-audio audio-streaming

我正尝试使用onerror来检测何时音频标签中提供的所有源均不可用/在线。出于某种奇怪的原因,它在Chrome和Firefox中无法运行,但在Safari,Edge和IE中可以正常运行。这背后的原因是什么,我该如何解决?

var audio = document.getElementById('audio');

audio.load();

// Can play
audio.oncanplay = function() {
  audio.play();
  console.log('Playing...');
};

// Cannot play
audio.onerror = function() {
  // error shows in Safari/IE/Edge but not in Chrome/Firefox
  console.log('Error! sources are not available');
};
<audio id="audio" controls>
  <source src="https://www.w3schools.com/html/horseX.ogg" type="audio/ogg">
  <source src="https://www.w3schools.com/html/horseX.mp3" type="audio/mpeg">
</audio>
<!-- Remove X from url for working mp3 samples -->

1 个答案:

答案 0 :(得分:1)

“错误”事件应附加在Google Chrome和Firefox中的源元素上。要处理多个源上的错误事件,必须将错误句柄绑定到最后一个源。