flex做的一個flv的簡單播放器代碼

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Style>
global{fontSize: 12;}
</mx:Style>
<mx:Script>
<![CDATA[
   import mx.controls.Alert;
   import mx.events.SliderEvent;
    import mx.events.VideoEvent;
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
      
   private var fExt:Array=[".flv",
    ".swf"];
   private var video_width:Number;
   private var video_height:Number;
   
   //private var _video:ArrayCollection;
      
    private var isplaying:Boolean=false;
      
      private var playPosition:Number;
      
       private var soundPosition:Number;   
   private function init():void
   {
    //
    hs.enabled=false;
    // stage.displayState = StageDisplayState.FULL_SCREEN;
   }

   private function to_play(event:Event):void
   {
    //
    if (this.file_list.selectedIndex==-1)
     {
      Alert.show("你沒有選擇播放文件.");
      return;
     }
    var sTemp:String;
    var sFile:File;
    sFile=File(this.file_list.selectedItem);
    sTemp=sFile.nativePath;
    //Alert.show(sTemp);
    my_video.source=sTemp;
   
    video_width=this.my_video.width;
    video_height=this.my_video.height;
    hs.enabled=true;
        //xml扩展,读取第一个XML第一个文件示例
        //my_video.source="videos/"+_video.getItemAt(0).filename;
        //trace(_video.getItemAt(0).filename);
        if(isplaying){
         my_video.pause();
         btn_play.label="播放"
        }else {
         my_video.play();
         btn_play.label="暫停"
        }
        isplaying = !isplaying;
        my_video.addEventListener(VideoEvent.PLAYHEAD_UPDATE, progressHandler);
   
   }
  
   private function to_play_2(event:Event):void
   {
    //
    if (this.file_list.selectedIndex==-1)
     {
      Alert.show("你沒有選擇播放文件.");
      return;
     }
    var sTemp:String;
    var sFile:File;
    sFile=File(this.file_list.selectedItem);
    sTemp=sFile.nativePath;
    //Alert.show(sTemp);
    my_video.source=sTemp;
   
    video_width=this.my_video.width;
    video_height=this.my_video.height;
    hs.enabled=true;
       btn_play.label="暫停"
       
        isplaying = true;
        my_video.addEventListener(VideoEvent.PLAYHEAD_UPDATE, progressHandler);
   
   }
   private function do_palyback(event:Event):void
   {
    var sFileTemp:String;
    if (this.rbtn_paly_1.selected)
    {
     sFileTemp=my_video.source;
     this.file_list.selectedIndex+=1;
     //if (this.file_list.selectedIndex==this.file_list.rowCount)
     // this.file_list.selectedIndex=0;
     var sTemp:String;
     var sFile:File;
     sFile=File(this.file_list.selectedItem);
     sTemp=sFile.nativePath;
     if (sTemp==sFileTemp)
     {
      this.file_list.selectedIndex=0;
      sFile=File(this.file_list.selectedItem);
      sTemp=sFile.nativePath;
     }
     //Alert.show(sTemp);
     my_video.source=sTemp;
     video_width=this.my_video.width;
     video_height=this.my_video.height;
    
     my_video.play();
    }
    else
    {
     this.my_video.play();
    
    }
   
    my_video.addEventListener(VideoEvent.PLAYHEAD_UPDATE, progressHandler);
   }
  
   private function video_stop(event:Event):void
   {
    this.my_video.stop();
    btn_play.label="播放"       
       isplaying = false;
   }
   private function v_room_big(event:Event):void
   {
    //
    //
    //Alert.show(String(this.file_list.rowCount));
   }
   private function v_room_small(event:Event):void
   {
    //
   
   }
   private function do_pause(event:Event):void
   {
    //
    this.my_video.pause();
    //Alert.show(String(video_width)+" 高"+String(video_height));
   
   }
  
   //播放器监听事件,从my_video里不停的得到值然后传输给HSLIDER
       private function progressHandler(event:VideoEvent):void
       {
        hs.value=my_video.playheadTime;
       }
      
       //拉动进度条
       private function thumbPress(event:SliderEvent):void
       {
        my_video.pause();
       }
       //进度条改变后,得到的值赋予PLAYPOSITION;
       private function thumbChanges(event:SliderEvent):void
       {
        if(my_video.playheadTime == -1){
      hs.value = 0;
      return;
     }
        playPosition = hs.value;
       }
      
       //放开进度条,再把PLAYPOSITION的值发给播放器;
       private function thumbRelease(event:SliderEvent):void
       {
        my_video.playheadTime = playPosition;
        if(isplaying){
         my_video.play();
        }else{
         my_video.pause();
        }
       }
       //声音音量控制           
       private function sound_thumbChanges(event:SliderEvent):void
       {
        soundPosition = hs_sound.value;
       }
      
   
       private function sound_thumbRelease(event:SliderEvent):void
       {
        my_video.volume = soundPosition;
       }
      
       //格式化时间
       private function formatTimes(value:int):String
       {
        var result:String = (value % 60).toString();
       
          if (result.length == 1){
              result = Math.floor(value / 60).toString() + ":0" + result;
          } else {
              result = Math.floor(value / 60).toString() + ":" + result;
          }
          return result;
       }
]]>
</mx:Script>
<mx:HDividedBox x="0" y="41" width="100%" height="100%">
   <mx:Panel width="220" height="100%" layout="absolute" id="play_file_list" title="播放列表">
    <mx:FileSystemList x="0" y="0" width="100%" height="60%" id="file_list" extensions="{fExt}" enumerationMode="filesOnly" doubleClick="to_play_2(event);"/>
    <mx:FileSystemTree id="file_tree"
    width="100%" height="36%"
    enumerationMode="directoriesOnly"   
    change="file_list.directory = File(file_tree.selectedItem);" x="0" bottom="0"/>
   </mx:Panel>
  
   <mx:Panel width="80%" height="100%" layout="absolute" id="play_windows">
    <mx:VideoDisplay x="0" y="0" width="100%" height="100%" id="my_video" backgroundColor="#FFFFFF" complete="do_palyback(event);"/>
   </mx:Panel>
</mx:HDividedBox>
<mx:ApplicationControlBar x="0" y="0" id="AppContBar" width="100%" height="43">
   <mx:RadioButtonGroup id="palyback_model"/>
   <mx:Button label="播放" id="btn_play" click="to_play(event);"/>
   <mx:Button label="停止" id="btn_stop" click="video_stop(event);"/>
   <mx:RadioButton label="連續播放" groupName="palyback_model" id="rbtn_paly_1" selected="true"/>
   <mx:RadioButton label="單曲重復播放" groupName="palyback_model" id="rbtn_paly_2"/>
   <mx:Button label="截圖" id="btn_Pic"/>
   <mx:Button label="+" id="btn_room_big" click="v_room_big(event);"/>
   <mx:Button label="-" id="btn_room_small" click="v_room_small(event);"/>
   <mx:HSlider id="hs" minimum="0" maximum="{my_video.totalTime}"
    thumbPress="thumbPress(event)"
    thumbRelease="thumbRelease(event)"
      change="thumbChanges(event)" />
   <mx:HSlider id="hs_sound" width="80"
      minimum="0" maximum="1"
    thumbRelease="sound_thumbRelease(event)"
      change="sound_thumbChanges(event)"
      value="{my_video.volume}" />
   <mx:Label id="playtime"
    text="{formatTimes(my_video.playheadTime)} : {formatTimes(my_video.totalTime)}"
    color="#ffffff"/>
</mx:ApplicationControlBar>

</mx:WindowedApplication>

 

 

在網上找的一段播放mp3

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    width="400" height="110" layout="vertical"
    horizontalAlign="center" verticalAlign="center"
>
    <mx:Script>
        <![CDATA[
            import mx.core.SoundAsset;
            import flash.media.*;

            [Embed(source="song.mp3")]
            [Bindable]
            public var Song:Class;
            public var mySong:SoundAsset = new Song() as SoundAsset;
            public var channel:SoundChannel;
           
            public function playSound():void
            {
                // 先 停止
                stopSound();
               
                // 在播放
                channel = mySong.play(0,int.MAX_VALUE);
            }
           
            public function stopSound():void
            {
                // 停止
                if ( channel != null ) channel.stop();
            }
        ]]>
    </mx:Script>
    <mx:HBox>
        <mx:Button label="play" click="playSound();"/>
        <mx:Button label="stop" click="stopSound();"/>       
    </mx:HBox>
</mx:Application>

posted @ 2009-07-16 21:33  谢多  阅读(417)  评论(0)    收藏  举报