歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> HTML5 Video DOM 入門體驗

HTML5 Video DOM 入門體驗

日期:2017/3/1 10:26:22   编辑:Linux編程

HTML5的一個新特性就是內置對多媒體的支持,<video> 元素很好用,也支持了不錯的API接口,下面用了一個案例來說明怎麼對<video> 元素的控制。

  1. <!DOCTYPE >
  2. <html>
  3. <head>
  4. <title></title>
  5. <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
  6. <script type="text/javascript">
  7. $(
  8. function() {
  9. $(":button").click(
  10. function() {
  11. var h;
  12. switch ($(":button").index($(this))) {
  13. case 0:
  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. case 1:
  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. case 2:
  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. "playing waiting ended play pause",
  60. function(e) {
  61. var vObj = 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. case e.target.error.MEDIA_ERR_ABORTED:
  78. $("ol>li:eq(3)").html("媒體資源獲取異常");
  79. break;
  80. case e.target.error.MEDIA_ERR_NETWORK:
  81. $("ol>li:eq(3)").html("網絡錯誤");
  82. break;
  83. case e.target.error.MEDIA_ERR_DECODE:
  84. $("ol>li:eq(3)").html("媒體解碼錯誤");
  85. break;
  86. case e.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. "suspend abort progress",
  98. function(e) {
  99. var vObj = 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. "progress error abort",
  106. function(e) {
  107. switch (e.target.readyState) {
  108. case 0:
  109. $("ol>li:eq(3)").html("當前播放位置無有效媒介資源");
  110. break;
  111. case 1:
  112. $("ol>li:eq(3)").html("加載中,媒介資源確認存在,但當前位置沒有能夠加載到有效媒介數據進行播放");
  113. break;
  114. case 2:
  115. $("ol>li:eq(3)").html("已獲取到當前播放數據,但沒有足夠的數據進行播放");
  116. break;
  117. case 3:
  118. $("ol>li:eq(3)").html("已獲取到後續播放數據,可以進行播放");
  119. break;
  120. default:
  121. case 4:
  122. $("ol>li:eq(3)").html("可以進行播放,且浏覽器確認媒體數據以某一種速度進行加載,可以保證有足夠的後續數據進行播放,而不會使浏覽器的播放進度趕上加載數據的末端");
  123. break;
  124. }
  125. console.log(e);
  126. }
  127. );
  128. }
  129. );
  130. </script>
  131. </head>
  132. <body>
  133. <video id="video1" autobuffer>
  134. <source src="video-test.mp4" type="video/mp4" />
  135. 對不起你的浏覽器不支持HTML5的新特性,要不你下載一個
  136. <a href="http://info.msn.com.cn/ie9/">IE9</a>
  137. </video>
  138. <input type="button" value="播放" disabled />
  139. <input type="button" value="縮小" disabled />
  140. <input type="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值,可以得到不同的狀態信息。具體的值,可以通過開發者工具獲得,或看相關文檔。

Copyright © Linux教程網 All Rights Reserved