【摘要】本文利用javascript脚本语言,设计并实现了一个网页版的Flash播放器。该播放器可实现网页中Flash动画的播放、停止、暂停和快进等功能。使用户能更好地控制网页中Flash动画的播放。
【关键词】Javascript;Flash;播放器;网页
1.引言
在用户浏览网页时,网页中的Flash动画(格式一般为swf)通常是自动播放的。浏览者是无法控制的,比如暂停、快进或重播等。这往往会影响用户浏览网页时的体验。本文实现了一个网页版的Flash播放器,该网页版Flash播放器能对网页中的Flash播放进行深度控制。
2.Flash播放器的实现
Flash播放器的控制代码用javascript实现,主要通过编写相应的函数控制flash动画的播放、暂停和快进等。该代码编写到一个脚本文件myFlash.js中,详细代码如下[1、2、3]。
function Flash_embedSWF(srcURL,swfbgColor){
vardefaultColor=(document.bgColor!=null)?document.bgColor:"#ffffff";
varbgcolor=(swfbgColor!=null)?swfbgColor:defaultColor;
document.writeln(
""+
""+
""+
""+
""+
""+
""+
"");
window.document.flash.Rewind();
}
function buttonClk()
{
switch(event.srcElement.value)
{
case "播放":
window.document.flash.Play();
break;
case "停止":
window.document.flash.Rewind();
oButton.style.pixelLeft=10;
break;
case "暂停":
window.document.flash.stopplay();
break;
case "快进":
if(window.document.flash.IsPlaying())
window.document.flash.StopPlay();
else
{ window.document.flash.gotoframe(window.document.flash.CurrentFrame()+50); oButton.style.pixelLeft=10+290*(oPlayer.CurrentFrame()+1)/oPlayer.TotalFrames;
}
break;
case "快退":
if(window.document.flash.IsPlaying())
window.document.flash.StopPlay();
else
{
window.document.flash.gotoframe(window.document.flash.CurrentFrame()-50); oButton.style.pixelLeft=10+290*(oPlayer.CurrentFrame()+1)/oPlayer.TotalFrames;
}
break;
}
}
接着制作一个简单的静态网页,调用上面的函数实现对Flash文件的控制。网页代码如下所示[4]。
Flash_embedSWF("12shengxiao.swf");
P
C
var _gaq = _gaq || [];_gaq.push(["_setAccount", "UA-22575914-1"]);_gaq.push(["_setDomainName", ".qikan.com"]);_gaq.push(["_trackPageview"]);(function () {var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);})();var _userid = "";var _siteid =708;var _istoken = 1;var _model = "Model03"; WebPageSpeed =438; UrchinTrack();
运行结果如图1所示。
四川省图书馆 function checkOS() { var ua = navigator.userAgent.split(";"); if (ua[2].indexOf("Windows NT 6.2") != -1) { document.getElementById("win8app").setAttribute("href", "http://apps.microsoft.com/webpdp/zh-CN/app/d2b8bfa0-dc29-4ad6-94a1-c0bc1ab4652c"); document.getElementById("win8app2").setAttribute("href", "http://apps.microsoft.com/webpdp/zh-CN/app/d2b8bfa0-dc29-4ad6-94a1-c0bc1ab4652c"); } else { document.getElementById("win8app").setAttribute("href", "javascript:alert("本App仅在Windows 8下运行!")"); document.getElementById("win8app2").setAttribute("href", "javascript:alert("本App仅在Windows 8下运行!")"); } } jQuery(document).ready(function () { jQuery("#nav").children().mouseover(function () { jQuery(this).addClass("over"); }).mouseout(function () { jQuery(this).removeClass("over"); }); jQuery("#navFooter").children().mouseover(function () { jQuery(this).addClass("over"); }).mouseout(function () { jQuery(this).removeClass("over"); }); //checkOS(); }); function doZoom(size) { document.getElementById("zoom").style.fontSize = size + "px"; } function doPrint() { var bdhtml = window.document.body.innerHTML; var sprnstr = ""; var eprnstr = "