ES 6 多媒体
JavaScript navigator对象包含一个子对象plugins。这个对象是一个数组,浏览器上安装的每个插件都有一个条目。该navigator.plugins对象只由Netscape,Firefox和Mozilla的支持。
例:
下面的示例显示如何列出浏览器中安装的所有插件。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>List of Plug-Ins</title> </head> <body> <table border = "1"> <tr> <th>插件名称</th> <th>文件名</th> <th>描述</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播放适当的多媒体文件。看一下下面的代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <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>标签来嵌入多媒体文件。
控制多媒体
让我们举一个适用于几乎所有浏览器的真实示例。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <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="https://public.xp.cn/down/course_demo/html/helloworld.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="开始" onclick="play();" /> <input type="button" value="停止" onclick="stop();" /> <input type="button" value="倒带" onclick="rewind();" /> </form> </body> </html>