溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HTML5 Video DOM 入門(mén)體驗(yàn)

發(fā)布時(shí)間:2020-06-06 18:37:40 來(lái)源:網(wǎng)絡(luò) 閱讀:446 作者:shyleoking 欄目:移動(dòng)開(kāi)發(fā)

HTML5的一個(gè)新特性就是內(nèi)置對(duì)多媒體的支持,<video> 元素很好用,也支持了不錯(cuò)的API接口,下面用了一個(gè)案例來(lái)說(shuō)明怎么對(duì)<video> 元素的控制。

  1. <!DOCTYPE > 
  2. <html> 
  3. <head> 
  4.     <title></title> 
  5.  
  6.     <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script> 
  7.  
  8.     <script type="text/javascript"> 
  9.         $(  
  10.         function() {  
  11.             $(":button").click(  
  12.             function() {  
  13.                 var h;  
  14.                 switch ($(":button").index($(this))) {  
  15.                     case 0:  
  16.                         if ($("video")[0].paused) {  
  17.                             $("video")[0].play();  
  18.                             $(this).val("暫停");  
  19.                         }  
  20.                         else {  
  21.                             $("video")[0].pause();  
  22.                             $(this).val("播放");  
  23.                         }  
  24.                         break;  
  25.                     case 1:  
  26.                         h = document.getElementsByTagName("video")[0].height == 0 ?  
  27.                             document.getElementsByTagName("video")[0].videoHeight - 10 :  
  28.                             document.getElementsByTagName("video")[0].height - 10; ;  
  29.                         document.getElementsByTagName("video")[0].height = h;  
  30.                         document.getElementsByTagName("video")[0].videoHeight = h;  
  31.                         break;  
  32.                     case 2:  
  33.                         h = document.getElementsByTagName("video")[0].height == 0 ?  
  34.                             document.getElementsByTagName("video")[0].videoHeight + 10 :  
  35.                             document.getElementsByTagName("video")[0].height + 10; ;  
  36.                         document.getElementsByTagName("video")[0].height = h;  
  37.                         document.getElementsByTagName("video")[0].videoHeight = h;  
  38.                         break;  
  39.                 }  
  40.             }  
  41.             );  
  42.         }  
  43.         );  
  44.  
  45.  
  46.         $(  
  47.         function() {  
  48.             $("#video1").on(  
  49.             "canplay",  
  50.             function(e) {  
  51.                 $(":button").removeAttr("disabled");  
  52.                 console.log(e);  
  53.             }  
  54.             );  
  55.             $("#video1").on(  
  56.             "canplaythrough",  
  57.             function(e) {  
  58.                 $("ol>li:eq(0)").html("全部加載完畢,你可以斷網(wǎng)看電影了!");  
  59.                 console.log(e);  
  60.             }  
  61.             );  
  62.             $("#video1").bind(  
  63.             "playing waiting ended play pause",  
  64.             function(e) {  
  65.                 var vObj = document.getElementById("video1");  
  66.                 $("ol>li:eq(1)").html(vObj.duration + ":" + vObj.startTime + ":" + vObj.currentTime);  
  67.                 console.log(e);  
  68.             }  
  69.             );  
  70.             $("#video1").on(  
  71.             "stalled",  
  72.             function(e) {  
  73.                 $("ol>li:eq(2)").html("你的網(wǎng)絡(luò)不給力啊,正在等數(shù)據(jù)呢");  
  74.                 console.log(e);  
  75.             }  
  76.             );  
  77.             $("#video1").on(  
  78.             "error",  
  79.             function(e) {  
  80.                 switch (e.target.error.code) {  
  81.                     case e.target.error.MEDIA_ERR_ABORTED:  
  82.                         $("ol>li:eq(3)").html("媒體資源獲取異常");  
  83.                         break;  
  84.                     case e.target.error.MEDIA_ERR_NETWORK:  
  85.                         $("ol>li:eq(3)").html("網(wǎng)絡(luò)錯(cuò)誤");  
  86.                         break;  
  87.                     case e.target.error.MEDIA_ERR_DECODE:  
  88.                         $("ol>li:eq(3)").html("媒體解碼錯(cuò)誤");  
  89.                         break;  
  90.                     case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:  
  91.                         $("ol>li:eq(3)").html("視頻格式被不支持");  
  92.                         break;  
  93.                     default:  
  94.                         $("ol>li:eq(3)").html("這個(gè)是神馬錯(cuò)誤啊");  
  95.                         break;  
  96.                 }  
  97.                 console.log(e);  
  98.             }  
  99.             );  
  100.             $("#video1").on(  
  101.             "suspend abort progress",  
  102.             function(e) {  
  103.                 var vObj = document.getElementById("video1");  
  104.                 $("ol>li:eq(1)").html(vObj.duration + ":" + vObj.startTime + ":" + vObj.currentTime);  
  105.                 console.log(e);  
  106.             }  
  107.             );  
  108.             $("#video1").on(  
  109.             "progress error abort",  
  110.             function(e) {  
  111.                 switch (e.target.readyState) {  
  112.                     case 0:  
  113.                         $("ol>li:eq(3)").html("當(dāng)前播放位置無(wú)有效媒介資源");  
  114.                         break;  
  115.                     case 1:  
  116.                         $("ol>li:eq(3)").html("加載中,媒介資源確認(rèn)存在,但當(dāng)前位置沒(méi)有能夠加載到有效媒介數(shù)據(jù)進(jìn)行播放");  
  117.                         break;  
  118.                     case 2:  
  119.                         $("ol>li:eq(3)").html("已獲取到當(dāng)前播放數(shù)據(jù),但沒(méi)有足夠的數(shù)據(jù)進(jìn)行播放");  
  120.                         break;  
  121.                     case 3:  
  122.                         $("ol>li:eq(3)").html("已獲取到后續(xù)播放數(shù)據(jù),可以進(jìn)行播放");  
  123.                         break;  
  124.                     default:  
  125.                     case 4:  
  126.                         $("ol>li:eq(3)").html("可以進(jìn)行播放,且瀏覽器確認(rèn)媒體數(shù)據(jù)以某一種速度進(jìn)行加載,可以保證有足夠的后續(xù)數(shù)據(jù)進(jìn)行播放,而不會(huì)使瀏覽器的播放進(jìn)度趕上加載數(shù)據(jù)的末端");  
  127.                         break;  
  128.                 }  
  129.                 console.log(e);  
  130.             }  
  131.             );  
  132.         }  
  133.         );  
  134.          
  135.       
  136.     </script> 
  137.  
  138. </head> 
  139. <body> 
  140.     <video id="video1" autobuffer> 
  141.     <source src="video-test.mp4" type="video/mp4" /> 
  142.     對(duì)不起你的瀏覽器不支持HTML5的新特性,要不你下載一個(gè)  
  143.     <a href="http://info.msn.com.cn/ie9/">IE9</a>?   
  144.   </video> 
  145.     <input type="button" value="播放" disabled /> 
  146.     <input type="button" value="縮小" disabled /> 
  147.     <input type="button" value="放大" disabled /> 
  148.     <ol> 
  149.         <li></li> 
  150.         <li></li> 
  151.         <li></li> 
  152.         <li></li> 
  153.         <li></li> 
  154.     </ol> 
  155. </body> 
  156. </html> 

對(duì) Video的控制重要的方法就是play、paused、stop。重要的事件有:

 

canplay 通知用戶可以播放了,但不一定資源全部下載好

canplaythrough 資源都下載完畢了
 

error 出錯(cuò)時(shí)候

事件參數(shù)中有一個(gè)target對(duì)象,他有一個(gè)readyState值,可以得到不同的狀態(tài)信息。具體的值,可以通過(guò)開(kāi)發(fā)者工具獲得,或看相關(guān)文檔。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI