JavaScript - 多媒體



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>

檢查外掛

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

  • 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">
      
      <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>
廣告