1

我有一个视频标签,下面是 html

<video  id="videoId" controls="controls">
<source src="../video/trailer.mp4" type="video/mp4" />
</video>

我想在这个视频元素上添加 onError 事件。我已经编写了触发但它不起作用的代码我该如何绑定它。可能是我错过了什么

window.onerror=function(){
            var myvid = document.getElementById('videoId');
            if (myvid.error) {
             switch (myvid.error.code) {
               case myvid.error.MEDIA_ERR_ABORTED:
                  alert("You stopped the video.");
                  break;
               case myvid.error.MEDIA_ERR_NETWORK:
                  alert("Network error - please try again later.");
                  break;
               case myvid.error.MEDIA_ERR_DECODE:
                  alert("Video is broken..");
                  break;
               case myvid.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
                  alert("Sorry, your browser can't play this video.");
                  break;
             }
            }
        }

提前感谢您的帮助。

4

2 回答 2

0

well, i m not sure if you can get the error over the window element... but if you look in the html5 specififation here: http://www.w3.org/TR/2010/WD-html5-20100624/video.html#video , you ll find following script block:

<script>
function failed(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;
 }
}
</script>
<p><video src="tgif.vid" autoplay controls onerror="failed(event)"></video></p>
<p><a href="tgif.vid">Download the video file</a>.</p>
于 2012-06-20T09:08:35.793 回答
0

重要的是,加载错误不是发生在视频标签上,而是发生在视频标签内的源标签上,并且事件必须附加到源标签的 src 属性,然后您可以使用 parentNode.id 来处理正确的视频标签并做一些事情错误发生的时间以及发生的确切位置。

我使用一系列带编号的视频标签和带有 Popcorn.js 对象的数组来处理页面上的视频。

这里有我的解决方案:

  $("source").error(function () {
    var tagStr = this.parentNode.id;
    var pop = videos[tagStr.charAt(str.length-1)];
    if (pop.media.networkState == pop.media.NETWORK_NO_SOURCE) {
      doSomething;
    }
  }).attr("src");
于 2012-08-08T13:24:06.740 回答