【发布时间】:2013-03-31 16:20:14
【问题描述】:
我希望为一个简单的 HTML5 视频元素添加一些错误处理。 我使用的这段代码无处不在 online:
JS
function playbackFailed(e) {
// video playback failed - show a message saying why
switch (e.target.error.code) {
case e.target.error.MEDIA_ERR_ABORTED:
alert('You aborted the video playback.');
break;
case e.target.error.MEDIA_ERR_NETWORK:
alert('A network error caused the video download to fail part-way.');
break;
case e.target.error.MEDIA_ERR_DECODE:
alert('The video playback was aborted due to a corruption problem or because the video used features your browser did not support.');
break;
case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
alert('The video could not be loaded, either because the server or network failed or because the format is not supported.');
break;
default:
alert('An unknown error occurred.');
break;
}
}
HTML
<video id="a" src="tgif.vid" autoplay controls onerror="playbackFailed(event)" poster="http://img.rasset.ie/000736d2-512.jpg" width="620" height="400"></video>
以上工作正常,当页面加载时我得到一个活泼的警报框。
但是,如果我没有“src”属性,而是在<video> 元素中使用<source> 标记,“onerror(event)”不会触发?示例标记:
<video id="b" autoplay controls onerror="playbackFailed(event)" poster="http://img.rasset.ie/000736d2-512.jpg" width="620" height="400">
<source src="tgif.vid">
</video>
请注意,我已在 id “a”和“b”上方给出了两个视频元素。
有人可以在下面的代码中解释原因吗:
<script>
var a = document.getElementById('a');
var b = document.getElementById('b');
a.addEventListener('error', function(e){alert('error event on a')});
b.addEventListener('error', function(e){alert('error event on b')});
</script>
我只收到“a”而不是“b”的警报
我需要使用<source> 标签,因为我将为不同的设备等提供多种媒体类型。
提前感谢任何答案/cmets
S
【问题讨论】:
-
你用其他浏览器验证了吗?
-
尝试将监听器添加到 b 的源标签而不是视频标签
标签: javascript html video error-handling