ES6 - 多媒體



JavaScript 的 navigator 物件包含一個名為 plugins 的子物件。此物件是一個數組,其中每個條目代表瀏覽器上安裝的每個外掛。navigator.plugins 物件僅受 Netscape、Firefox 和 Mozilla 支援。

示例

以下示例演示如何列出瀏覽器中安裝的所有外掛。

<html> 
   <head> 
      <title>List of Plug-Ins</title> 
   </head> 
   <body> 
      <table border = "1"> 
         <tr>
            <th>Plug-in Name</th>
            <th>Filename</th>
            <th>Description</th>
         </tr> 
         <script LANGUAGE = "JavaScript" type = "text/javascript"> 
            for (i = 0; i<navigator.plugins.length; i++) {    
               document.write("<tr><td>");  
               document.write(navigator.plugins[i].name);    
               document.write("</td><td>");  
               document.write(navigator.plugins[i].filename); 
               document.write("</td><td>");  
               document.write(navigator.plugins[i].description);    
               document.write("</td></tr>");  
            }  
         </script> 
      </table> 
   </body>
   
</html>

輸出

在成功執行上述程式碼後,將顯示以下輸出。

pluggins list

檢查外掛

每個外掛在陣列中都有一個條目。每個條目都具有以下屬性:

  • name - 外掛的名稱。

  • filename - 載入以安裝外掛的可執行檔案。

  • description - 開發人員提供的外掛描述。

  • mimeTypes - 包含外掛支援的每個 MIME 型別的條目的陣列。

您可以在指令碼中使用這些屬性來查詢已安裝的外掛,然後使用 JavaScript 播放相應的媒體檔案。請檢視以下程式碼。

<html> 
   <head> 
      <title>Using Plug-Ins</title> 
   </head> 
   
   <body> 
      <script language = "JavaScript" type = "text/javascript"> 
         media  =  navigator.mimeTypes["video/quicktime"]; if (media) {  
            document.write("<embed src = 'quick.mov' height = 100 width = 100>");  
         } else {  
            document.write("<img src = 'quick.gif' height = 100 width = 100>");  
         }  
      </script> 
   </body>
</html>

注意 - 在這裡,我們使用 HTML <embed> 標籤來嵌入多媒體檔案。

控制多媒體

讓我們舉一個幾乎在所有瀏覽器中都能正常工作的真實示例。

<html> 
   <head> 
      <title>Using Embeded Object</title> 
      
      <script type = "text/javascript"> 
         <!--  
            function play() {  
               if (!document.demo.IsPlaying()) {      
                  document.demo.Play();  
               }  
            }  
            function stop() {  
               if (document.demo.IsPlaying()){      
                  document.demo.StopPlay();  
               }  
            }  
            function rewind() { 
               if (document.demo.IsPlaying()){      
                  document.demo.StopPlay();  
               }  
               document.demo.Rewind();  
            } 
            //
         --> 
      </script> 
   </head> 
   <body> 
      <embed id = "demo" name = "demo"
         src = "http://www.amrood.com/games/kumite.swf" 
         width = "318" height = "300" play = "false" loop = "false"     
         pluginspage = "http://www.macromedia.com/go/getflashplayer"     
         swliveconnect = "true"> 
      </embed> 
      
      <form name = "form" id = "form" action = "#" method = "get"> 
         <input type = "button" value = "Start" onclick = "play();" /> 
         <input type = "button" value = "Stop" onclick = "stop();" /> 
         <input type = "button" value = "Rewind" onclick = "rewind();" /> 
      </form>
   </body> 
</html>
廣告