`
bcyy
  • 浏览: 1829580 次
文章分类
社区版块
存档分类
最新评论

HTML5 Video DOM 入门体验

 
阅读更多

HTML5的一个新特性就是内置对多媒体的支持,<video> 元素很好用,也支持了不错的API接口,下面用了一个案例来说明怎么对<video> 元素的控制。

  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <title></title>
  5. <scriptsrc="js/jquery-1.7.1.min.js"type="text/javascript"></script>
  6. <scripttype="text/javascript">
  7. $(
  8. function(){
  9. $(":button").click(
  10. function(){
  11. varh;
  12. switch($(":button").index($(this))){
  13. case0:
  14. if($("video")[0].paused){
  15. $("video")[0].play();
  16. $(this).val("暂停");
  17. }
  18. else{
  19. $("video")[0].pause();
  20. $(this).val("播放");
  21. }
  22. break;
  23. case1:
  24. h=document.getElementsByTagName("video")[0].height==0?
  25. document.getElementsByTagName("video")[0].videoHeight-10:
  26. document.getElementsByTagName("video")[0].height-10;;
  27. document.getElementsByTagName("video")[0].height=h;
  28. document.getElementsByTagName("video")[0].videoHeight=h;
  29. break;
  30. case2:
  31. h=document.getElementsByTagName("video")[0].height==0?
  32. document.getElementsByTagName("video")[0].videoHeight+10:
  33. document.getElementsByTagName("video")[0].height+10;;
  34. document.getElementsByTagName("video")[0].height=h;
  35. document.getElementsByTagName("video")[0].videoHeight=h;
  36. break;
  37. }
  38. }
  39. );
  40. }
  41. );
  42. $(
  43. function(){
  44. $("#video1").on(
  45. "canplay",
  46. function(e){
  47. $(":button").removeAttr("disabled");
  48. console.log(e);
  49. }
  50. );
  51. $("#video1").on(
  52. "canplaythrough",
  53. function(e){
  54. $("ol>li:eq(0)").html("全部加载完毕,你可以断网看电影了!");
  55. console.log(e);
  56. }
  57. );
  58. $("#video1").bind(
  59. "playingwaitingendedplaypause",
  60. function(e){
  61. varvObj=document.getElementById("video1");
  62. $("ol>li:eq(1)").html(vObj.duration+":"+vObj.startTime+":"+vObj.currentTime);
  63. console.log(e);
  64. }
  65. );
  66. $("#video1").on(
  67. "stalled",
  68. function(e){
  69. $("ol>li:eq(2)").html("你的网络不给力啊,正在等数据呢");
  70. console.log(e);
  71. }
  72. );
  73. $("#video1").on(
  74. "error",
  75. function(e){
  76. switch(e.target.error.code){
  77. casee.target.error.MEDIA_ERR_ABORTED:
  78. $("ol>li:eq(3)").html("媒体资源获取异常");
  79. break;
  80. casee.target.error.MEDIA_ERR_NETWORK:
  81. $("ol>li:eq(3)").html("网络错误");
  82. break;
  83. casee.target.error.MEDIA_ERR_DECODE:
  84. $("ol>li:eq(3)").html("媒体解码错误");
  85. break;
  86. casee.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
  87. $("ol>li:eq(3)").html("视频格式被不支持");
  88. break;
  89. default:
  90. $("ol>li:eq(3)").html("这个是神马错误啊");
  91. break;
  92. }
  93. console.log(e);
  94. }
  95. );
  96. $("#video1").on(
  97. "suspendabortprogress",
  98. function(e){
  99. varvObj=document.getElementById("video1");
  100. $("ol>li:eq(1)").html(vObj.duration+":"+vObj.startTime+":"+vObj.currentTime);
  101. console.log(e);
  102. }
  103. );
  104. $("#video1").on(
  105. "progresserrorabort",
  106. function(e){
  107. switch(e.target.readyState){
  108. case0:
  109. $("ol>li:eq(3)").html("当前播放位置无有效媒介资源");
  110. break;
  111. case1:
  112. $("ol>li:eq(3)").html("加载中,媒介资源确认存在,但当前位置没有能够加载到有效媒介数据进行播放");
  113. break;
  114. case2:
  115. $("ol>li:eq(3)").html("已获取到当前播放数据,但没有足够的数据进行播放");
  116. break;
  117. case3:
  118. $("ol>li:eq(3)").html("已获取到后续播放数据,可以进行播放");
  119. break;
  120. default:
  121. case4:
  122. $("ol>li:eq(3)").html("可以进行播放,且浏览器确认媒体数据以某一种速度进行加载,可以保证有足够的后续数据进行播放,而不会使浏览器的播放进度赶上加载数据的末端");
  123. break;
  124. }
  125. console.log(e);
  126. }
  127. );
  128. }
  129. );
  130. </script>
  131. </head>
  132. <body>
  133. <videoid="video1"autobuffer>
  134. <sourcesrc="video-test.mp4"type="video/mp4"/>
  135. 对不起你的浏览器不支持HTML5的新特性,要不你下载一个
  136. <ahref="http://info.msn.com.cn/ie9/">IE9</a>
  137. </video>
  138. <inputtype="button"value="播放"disabled/>
  139. <inputtype="button"value="缩小"disabled/>
  140. <inputtype="button"value="放大"disabled/>
  141. <ol>
  142. <li></li>
  143. <li></li>
  144. <li></li>
  145. <li></li>
  146. <li></li>
  147. </ol>
  148. </body>
  149. </html>
对Video的控制重要的方法就是play、paused、stop。重要的事件有:

canplay 通知用户可以播放了,但不一定资源全部下载好

canplaythrough 资源都下载完毕了

error 出错时候

事件参数中有一个target对象,他有一个readyState值,可以得到不同的状态信息。具体的值,可以通过开发者工具获得,或看相关文档。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics