一个silverlight播放器的DEMO(Expression Encoder 2)

2016-08-20 10:37:33来源:http://daizhj.blog.51cto.com/285189/86909作者:daizhenjun人点击


在写完"Silverlight中鼠标事件的js开发"一文之后,我就想写一篇真正应用这种事件处理方式的进行实际开发的例子。后来在网上看到了这篇文章(实际应用链接:[url]http://www.thejamesbondmovies.com/[/url]),发现里面的DEMO正好是我想演示的。如下: 因为这个DEMO的演示用到了Expression Encoder, 而相关下载链接, 请点击这里。 原文中的老外因为不满足于Expression Encoder所提供的模板代码,他认为在媒体列表中应该实现点播的效果。而不仅仅是“一味”的顺序播放。因此他在Expression Encoder代码生成的基础上,对Xaml和js文件都做了相应改动。下面我们来看一下他所开发的主要流程。 首先使用Expression Encoder来创建一个项目,并将其模板设置为"Expression" (因为本人不喜欢这个模板,所以在DEMO中使用了"Executive"模板),然后单击Import按钮来选择要播放的视频文件。如下图所示。最后单击“Encode”进行编码转换(当转换成功后会启动一个测试页面进行演示,“Preview In Brower”需勾选)。 这样我们就可以在“Directory”设置项中下找到我们所生成的所有项目文件了。 因为模板文件生成的XAML中不带播放列表,所以要修改XAML文件中Canvas的高度,并将下面的XAML代码(播入列表代码)放在该文件的结尾处:<!--Playlistregionstartshere--><!--NavigationArrows--><Pathx:Name="LeftArrow"Opacity="0.74"Width="38"Height="38"Stretch="Fill"Stroke="#FF000000" Canvas.Left="11"Canvas.Top="514"Data="M37.5,0.5L37.5,37.50.5,37.5z"Fill="#FFFFFFFF" RenderTransformOrigin="0.5,0.5"Cursor="Hand"><Path.RenderTransform><TransformGroup><ScaleTransformScaleX="1"ScaleY="1"/><SkewTransformAngleX="0"AngleY="0"/><RotateTransformAngle="134.119"/><TranslateTransformX="0"Y="0"/></TransformGroup></Path.RenderTransform></Path><Pathx:Name="RightArrow"Opacity="0.74"Width="38"Height="38"Stretch="Fill"Stroke="#FF000000" Canvas.Left="588"Canvas.Top="514"Data="M37.5,0.5L37.5,37.50.5,37.5z"Fill="#FFFFFFFF" RenderTransformOrigin="0.5,0.5"Cursor="Hand"><Path.RenderTransform><TransformGroup><ScaleTransformScaleX="1"ScaleY="1"/><SkewTransformAngleX="0"AngleY="0"/><RotateTransformAngle="314.365"/><TranslateTransformX="0"Y="0"/></TransformGroup></Path.RenderTransform></Path><!--Theoutercanvashereisclipped:onlytheareadefinedbytherectanglegeometryisvisible--><!--Thisisnecessaryaswhenweanimatethe'Library'canvasinsideitwedonotwanttoseethe thumbnailsslideunderthenavigationarrowsandoffthescreen--><Canvasx:Name="ClippedCanvas"Canvas.Top="491"Canvas.Left="43"Width="550"Height="90" Background="Silver"><Canvas.Clip><RectangleGeometryRect="0,0,550,114"/></Canvas.Clip><!--Animationstomovetheplaylistleftandright.Theyarenumberedsothatwecancallthem logicallyfromcode--><Canvas.Resources><Storyboardx:Name="MoveLeft01"><DoubleAnimationStoryboard.TargetProperty="(Canvas.Left)"Storyboard.TargetName="Library" From="13"To="-613"Duration="0:0:2"/></Storyboard><Storyboardx:Name="MoveRight02"><DoubleAnimationStoryboard.TargetProperty="(Canvas.Left)"Storyboard.TargetName="Library" From="-613"To="13"Duration="0:0:2"/></Storyboard></Canvas.Resources><!--TheLibraryCanvasgroupstheplaylistbuttonsintoasingleelementthatcanbeeasilyanimated left-right.--><CanvasWidth="1157.275"Height="82.96"Canvas.Left="0"Canvas.Top="0"x:Name="Library"><CanvasWidth="550.275"Height="82.96"x:Name="playlist1"><Imagex:Name="play0"Opacity="0.74"Width="133.275"Height="82.96"Source="1.png" Stretch="Fill"Cursor="Hand"/><Imagex:Name="play1"Opacity="0.74"Width="133.275"Height="82.96"Source="2.png" Stretch="Fill"Cursor="Hand"Canvas.Left="139"/><Imagex:Name="play2"Opacity="0.74"Width="133.275"Height="82.96"Source="3.png" Stretch="Fill"Cursor="Hand"Canvas.Left="278"/><Imagex:Name="play3"Opacity="0.74"Width="133.275"Height="82.96"Source="4.png" Stretch="Fill"Cursor="Hand"Canvas.Left="417"/></Canvas><CanvasWidth="550.275"Height="82.96"x:Name="playlist2"Canvas.Left="607"><Imagex:Name="play4"Opacity="0.74"Width="133.275"Height="82.96"Source="5.png" Stretch="Fill"Cursor="Hand"/><Imagex:Name="play5"Opacity="0.74"Width="133.275"Height="82.96"Source="6.png" Stretch="Fill"Cursor="Hand"Canvas.Left="139"/><Imagex:Name="play6"Opacity="0.74"Width="133.275"Height="82.96"Source="7.png" Stretch="Fill"Cursor="Hand"Canvas.Left="278"/><Imagex:Name="play7"Opacity="0.74"Width="133.275"Height="82.96"Source="8.png" Stretch="Fill"Cursor="Hand"Canvas.Left="417"/></Canvas></Canvas></Canvas> 下面就要改一下相应的js文件了,找到StartPlayer.js文件,新的播入列表方法放在页面的最上面://全局变量声明varcurPos=1;//当前播入的媒体文件在列表中的位置varmaxPos=2;//当前的分页数,因为是8个文件,每4个文件为1页,所以这里是两页varcVideos=8;//视频文件个数(本DEMO中有8个)functionget_mediainfo(mediainfoIndex){switch(mediainfoIndex){case0:return{"mediaSource":"Movie1.wmv","placeholderSource":"","chapters":[]};case1:return{"mediaSource":"Movie2.wmv","placeholderSource":"","chapters":[]};case2:return{"mediaSource":"Movie3.wmv","placeholderSource":"","chapters":[]};case3:return{"mediaSource":"Movie4.wmv","placeholderSource":"","chapters":[]};case4:return{"mediaSource":"Movie5.wmv","placeholderSource":"","chapters":[]};case5:return{"mediaSource":"Movie6.wmv","placeholderSource":"","chapters":[]};case6:return{"mediaSource":"Movie7.wmv","placeholderSource":"","chapters":[]};case7:return{"mediaSource":"Movie8.wmv","placeholderSource":"","chapters":[]};default:throwError.invalidOperation("Nosuchmediainfo");}} 将原码中的原有播放文件列表变量和相关内容注释://this._playlist=[];//try{//eval('this._playlist=['+//'{"mediaSource":"15_mt_l2s_callingsqlfunctions.wmv",'+//'"placeholderSource":"",'+//'"chapters":'+//'['+//']},'+//'{"mediaSource":"18_mt_l2s_transactions.wmv",'+//'"placeholderSource":"",'+//'"chapters":'+//'['+//']}'+//'];');//}//catch(e){}//this._galleryItems=[];//try{//eval('this._galleryItems=['+//'newExpressionPlayer.GalleryItem("15_mt_l2s_callingsqlfunctions.wmv",""),'+//'newExpressionPlayer.GalleryItem("18_mt_l2s_transactions.wmv","")'+//'];');//}//catch(e){}//this._player.set_galleryInfo(this._galleryItems,Function.createDelegate(this,this._onClickGalleryItem));//this._onPlayNextVideo(null,null); 将下列代码放在该注释下方来解决对相应媒体播放按钮进行事件绑定://wireuptherolloverandclickeventsforeachofourplaybuttonsthis.Plugin=document.getElementById(this._hostname);for(vari=0;i<cVideos;i++){varelement=this.Plugin.Content.findName('play'+i);element.addEventListener("MouseEnter",Function.createDelegate(this,this._rollOver));element.addEventListener("MouseLeave",Function.createDelegate(this,this._rollOut));element.addEventListener("MouseLeftButtonUp",Function.createDelegate(this,this._playX));}this.Plugin.Content.findName('LeftArrow').addEventListener("MouseEnter", Function.createDelegate(this,this._rollOver));this.Plugin.Content.findName('LeftArrow').addEventListener("MouseLeave", Function.createDelegate(this,this._rollOut));this.Plugin.Content.findName('LeftArrow').addEventListener("MouseLeftButtonUp", Function.createDelegate(this,this._slideLeft));this.Plugin.Content.findName('RightArrow').addEventListener("MouseEnter", Function.createDelegate(this,this._rollOver));this.Plugin.Content.findName('RightArrow').addEventListener("MouseLeave", Function.createDelegate(this,this._rollOut));this.Plugin.Content.findName('RightArrow').addEventListener("MouseLeftButtonUp", Function.createDelegate(this,this._slideRight));this._onPlayNextVideo(null,null); 而下面就是其相应的事件处理代码了:_rollOver:function(sender,eventArgs){sender.opacity=1;},_rollOut:function(sender,eventArgs){sender.opacity=0.74;},_playX:function(sender,eventArgs){varX=Number(sender.Name.substring(4));this._currentMediainfo=X;this._player.set_mediainfo(get_mediainfo(X));sender.opacity=1;},_slideLeft:function(sender,eventArgs){switch(curPos){case1:sender.findName("LeftArrow").opacity=0.85;break;default:sender.findName("MoveRight0"+curPos).Begin();curPos--;}},_slideRight:function(sender,eventArgs){switch(curPos){casemaxPos:sender.findName("RightArrow").opacity=0.85;break;default:sender.findName("MoveLeft0"+curPos).Begin();curPos++;sender.findName("LeftArrow").opacity=1;}} 因为使用了新的播放列表变量,所以原来生成的如下方法内容会被改写如下:_onPlayPreviousVideo:function(sender,eventArgs){if(this._currentMediainfo>0){this._player.set_mediainfo(get_mediainfo(--this._currentMediainfo));}},_onPlayNextVideo:function(sender,eventArgs){if(this._currentMediainfo<cVideos){this._player.set_mediainfo(get_mediainfo(++this._currentMediainfo));}}, 到这里我们可以在本地运行一下default.htm来看一下效果。 然后为了演示方便,我将这个Application上传到了Silverlight Streaming上,经常了n遍的上传之后,终于测试成功,所以才在本文开头做了相应的演示。 好了,今天的内容就先到这里了. 源码下载,请点击这里:) 注:因为老外所写的文章是在silverlight 1.0下运行的,所以本文的JS与原文中有所变动,但不会影响对代码的理解。Tag标签: silverlight,js,daizhj,代震军

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台