flex4.5 for VideoPlayer 简单应用

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="540" minHeight="405" initialize="init();"  creationComplete="initApp();">

<s:VideoPlayer id="vp" x="0" y="0" width="540" height="405" autoPlay="false" autoRewind="false"
       bytesLoadedChange="vp_bytesLoadedChangeHandler(event)" loop="true"
       mediaPlayerStateChange="vp_mediaPlayerStateChangeHandler(event)" source="{videoSource}"/>
 
 <s:SWFLoader id="btnstop1" x="1" y="1" click="btnPlay()"  source="images/bg1.swf"/>
 <s:SWFLoader id="btnplay" x="1" y="1"  click="btnbgclick()"  source="images/bg.swf" />
 
 <s:SWFLoader id="swfload" x="66" y="36" width="550" height="400" source="images/loading.swf"/>

<fx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.events.DragEvent;
   
   import org.osmf.events.LoadEvent;
   import org.osmf.events.MediaPlayerStateChangeEvent;
   import org.osmf.events.TimeEvent;
   
   [Bindable] 
   private var videoSource:String;         //媒体路径
   [Bindable] 
   private var playPosition:Number;            //播放进度  
   
   private var flag:Boolean;
   protected function init():void{
    
    flag=false;
    
  
    btnplay.visible=false;
    btnstop1.visible=false;
    //videoSource="images/WebCast20100901_Video2.mp4";
       videoSource = parameters.videoSource;
   //Alert.show(parameters.videoSource);
     
      //右键信息添加
      var contextMenu:ContextMenu=new ContextMenu();
      contextMenu.hideBuiltInItems();
     
      var jinherMenuItem:ContextMenuItem=new ContextMenuItem("金和软件股份有限公司");
      jinherMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemHandler);
     
      var versionMenuItem:ContextMenuItem=new ContextMenuItem("版本号 version 1.0");
     
     
      contextMenu.customItems.push(jinherMenuItem);
      contextMenu.customItems.push(versionMenuItem);
     
      //为上下文菜单设置监听
      contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,menuHandler);

      this.contextMenu=contextMenu;
     
   }
   //菜单监听处理函数
   internal function menuHandler(evt:ContextMenuEvent):void{
    trace("menu");
   }
   //子菜单项监听处理函数
   internal function menuItemHandler(evt:ContextMenuEvent):void{
    //获取事件源,斌将其转化为ContextMenuItem
    var item:ContextMenuItem=ContextMenuItem(evt.currentTarget);
    var caption:String=item.caption;
    
      navigateToURL(new URLRequest("http://www.jinher.com/"),"_blank");
     
   }

   
   //设置初始播放位置
   protected function vp_bytesLoadedChangeHandler(event:LoadEvent):void
   {
    //Alert.show("正在下载的"+vp.videoDisplay.bytesLoaded+"总大小"+ vp.videoDisplay.bytesTotal)
    //当下载大小与总大小相同时
    if(vp.videoDisplay.bytesLoaded==vp.videoDisplay.bytesTotal)
    {
     vp.videoDisplay.seek(parameters.playPosition);
     swfload.visible=false;
     btnplay.visible=true;
     
    
    }
    else
    {
     swfload.visible=true; 
     btnplay.visible=true;
     
    }
    
    //开始播放
    vp.autoPlay=true;
    //允许循环播放
    vp.loop=true;
    //播放状态
    if(vp.playing)
    {
     swfload.visible=false;
   
     btnplay.visible=true;
     
     

    }
    //暂停状态
    else
    {
     btnplay.visible=false;
     
     
    }
    btnstop1.visible=false; 
   }
   //返回视频播放进度值
   private function GetVideoTime():Number{
    return vp.currentTime;
   }
   //与javascript交互用传递视频播放进度值
   private function initApp():void{
    
    flash.external.ExternalInterface.addCallback("GetVideoTime",GetVideoTime);
   }
   
   //通过判断播放状态来设定
   protected function vp_mediaPlayerStateChangeHandler(event:MediaPlayerStateChangeEvent):void
   {
    //如果是播放状态
    if(vp.playing)
    {
     btnplay.visible=true;
     btnstop1.visible=false;
     swfload.visible=false;
    }
    //不是播放状态
    else 
    {
     //进度加载完成
     if(vp.videoDisplay.bytesLoaded==vp.videoDisplay.bytesTotal)
     {
     
      //没有播放
      if(!vp.playing){
       btnplay.visible=false;
       if(flag){
        btnstop1.visible=true;
       }
       else
       {
        btnplay.visible=true;
       }
       swfload.visible=true;
       
      }
      //播放状态
      else
      {
       btnplay.visible=true;
       btnstop1.visible=false;
      }
     }
     //进度没有加载完成
     else
     {
      btnplay.visible=true;
      btnstop1.visible=false;
     }
    }
    
  
   
   }
            //播放
   protected function btnPlay():void{
    
    swfload.visible=false;
    vp.videoDisplay.seek(parameters.playPosition);
    if(flag)
    {
     //暂停
     vp.stop();
     vp.videoDisplay.seek(vp.currentTime);
    }
    //播放
    vp.play();
    btnplay.visible=false;
    btnstop1.visible=true;
       if(!flag){btnstop1.visible=false;btnplay.visible=true;}
    
   }
   //暂停播放
   protected function btnstop():void{
    flag=true;
    vp.pause();
   
    btnplay.visible=false;
    btnstop1.visible=true;
    swfload.visible=true;
   }
   //点击屏幕时播放与暂定动作
   protected function btnbgclick():void{
    if(vp.playing)
    {
    //暂停
     btnstop();
    
    }
    else
    {
    //播放
     btnPlay();
    
    }
   }
   
  ]]>
 </fx:Script>

本文出自 “tskpcp” 博客,转载请与作者联系!

你可能感兴趣的:(Flex,职场,休闲,4.5,videoplayer播放器)