我的flex MP3 播放器

来源:互联网 发布:linux虚拟机代理上网 编辑:程序博客网 时间:2024/04/30 18:41

做了个基于flex技术的mp3播放器,样子有点像谷歌音乐里的播放器(O(∩_∩)O~)。功能还不是很全,但也先摆在这吧!

这是mxml文件:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">
<mx:Script>
 <![CDATA[
  import mx.formatters.DateFormatter;
  import mx.collections.ArrayCollection;
  import mx.events.TreeEvent;
  import mx.controls.Alert;
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  import flash.media.Sound;
  import flash.media.SoundChannel;
  import flash.media.SoundMixer;
  import flash.media.SoundTransform;
  [Bindable]
  public var musicMenu:XML;   //all the songs.
  public var loader:URLLoader;
  [Bindable]
  public var playMenu:XML;   //the song selected from the left tree;
  private var count:int=0;   //number of songs
  
  public var mySong:Sound;
  public var songChannel:SoundChannel;
  public var sTransform:SoundTransform;
  public var location:Number;
  public var songURL:String;
  //[Bindable]
  //public var songTime:String='0';
  public var songLength:int;
  public var currentSongIndex:int;
  public var ifPause:Boolean=false;  //if it is playing.
  public var playMethod:int=1;   //1,循环播放;2,单曲循环;3,随即播放
  public var timer:Timer=new Timer(5,0);
  
  public function init():void{
   loader=new URLLoader();
   loader.dataFormat=URLLoaderDataFormat.TEXT;
   loader.load(new URLRequest("music.xml"));
   loader.addEventListener(Event.COMPLETE,onComplet);
   
   playMenu=
     <music />         //初始化playMenu.
      
   playLs.selectedIndex=0;  //让datagrid默认选中第一个。
   
   //初始化音量
   sTransform=new SoundTransform(0.5);
  }
  
  //this function to control the processBar
  public function processLevel(evt:TimerEvent):void{
   var i:int;
   var lab:String;
   playBar.setProgress(songChannel.position,mySong.length);
   lab=((songChannel.position/mySong.length)*100).toString();
   i=lab.indexOf('.');
   lab=lab.substring(0,i+2);
   playBar.label=lab+"%";
  }
  
  public function onComplet(evt:Event):void{
   musicMenu=new XML(evt.target.data);   
   songLength=musicMenu.item.length();
  }
  
  public function selectHandler(event:Event):void{
   playMenu.appendChild(tree.selectedItem.toXMLString());
            //Alert.show(tree.selectedItem.toXMLString());   能显示出来
   ///Alert.show(playMenu.toXMLString());   ? %>_<%
     //以前一直失败,是因为没加根节点,呵呵!
  } 
  
  public function buttonClick():void{
   if(playAndPause.source=="pic/play.gif")
   playMusic();
   else
   pause(); 
  }
  
  public function newSong():void{
   if(songChannel!=null)
   songChannel.stop();
   
   ifPause=false;
   playMusic();
  }
  
  public function playMusic():void{
   playAndPause.source="pic/pause.gif"; 
   if(!ifPause){                    //the status is playing.
    if(playLs.selectedIndex==-1)
     playLs.selectedIndex=0;       //如果没选中歌曲就选第一个。
    songURL=playLs.selectedItem.@sourse;
    currentSongIndex=playLs.selectedIndex;
    mySong=new Sound();
    mySong.load(new URLRequest(songURL));
    songChannel=mySong.play();
    songChannel.soundTransform=sTransform;
    songChannel.addEventListener(Event.SOUND_COMPLETE,playComplet);
    
    //songTime=(mySong.length/1000/60).toString().substr(0,3);
   }
   else{                              //the status is pause.
    songChannel=mySong.play(location);
   }
   
   ifPause=false; 
   
   if(!timer.hasEventListener(TimerEvent.TIMER)){
    timer.addEventListener(TimerEvent.TIMER,processLevel);
    timer.start();
   } 
  }
  
    
  public function pause():void{
   ifPause=true;
   location=songChannel.position;
   songChannel.stop();
   playAndPause.source="pic/play.gif";
  }
  
  public function jump(der:int):void{
   songChannel.stop();

   if(der==1&&playLs.selectedIndex!=songLength-1)
   playLs.selectedIndex=playLs.selectedIndex+1;
   
   if(der==-1&&playLs.selectedIndex!=0)
   playLs.selectedIndex=playLs.selectedIndex-1;
   
   ifPause=false;
   playMusic();
  }
    
     ///在歌曲播放完调度
  public function playComplet(event:Event):void{
    ifPause=false;
    switch(playMethod){
     case 1:playLs.selectedIndex=(currentSongIndex+1)%songLength;break;
     case 2:break;
     case 3:break;    
    }
    playMusic();  
  }
  
  //调整音量
  public function justVolum(evt:Event):void{
   sTransform.volume=evt.target.value;
   //songChannel.soundTransform=new SoundTransform(evt.target.value);
   songChannel.soundTransform=sTransform;
  }
 ]]>
</mx:Script>

<mx:Style>
 Application{
  layout:vertical;
  backgroundColor:#ffffff;
  borderStyle:solid;
 }
 
 Canvas{
  cornerRadius:8;
  borderStyle:solid;
  borderColor:#999999;
 }
 
 .caBot{
  leftPadding:10;
  topPadding:10;
  bottomPadding:30;
  rightBottom:10;
 }
 
 global{
  fontSize:12; 
 }

 Panel{
  borderThicknessBottom:1;
  borderThicknessLeft:1;
  borderThicknessRight:1;
 }
 
 Button{
  color:#999999;
  
 }
</mx:Style>
 <mx:HTTPService id="svc" url="music.xml" resultFormat="e4x"/>
       
 <mx:VBox width="750" height="500">
  <mx:Canvas id="top" width="100%" height="70">
   <mx:Image source="@Embed(source='pic/back.gif')" y="18" x="30" id="goon" click="jump(-1)"/>
   <mx:Image source="pic/play.gif" y="18" x="70" click="buttonClick()" id="playAndPause" />
   <mx:Image source="@Embed(source='pic/goon.gif')" y="18" x="110" id="back" click="jump(1)"/>
   <mx:Image source="@Embed(source='pic/volum.gif')" y="26" x="467"/>
   <mx:ProgressBar mode="manual" labelPlacement="top" label="0%" themeColor="#9999ff" color="#999999"
     direction="right" y="13" x="162" width="282" fontSize="10" id="playBar"/>
   <mx:Button x="647" y="40" label="循环播放" width="77" height="22" fontFamily="Arial"
    fillAlphas="[1.0, 1.0]" fillColors="[#FFFFFF, #FFFFFF]"
    click="playMethod=1"/>
   <mx:Button x="647" y="10" label="单曲循环" width="77" height="22" fontFamily="Arial"
    fillAlphas="[1.0, 1.0]" fillColors="[#FFFFFF, #FFFFFF]"
    click="playMethod=2"/>
   <mx:HSlider x="494" y="26" width="88" minimum="0" maximum="1" snapInterval="0.1"
     showDataTip="false" change="justVolum(event)" liveDragging="true" value="0.5"/>
  </mx:Canvas>
  
<mx:Canvas id="bottom" width="100%" height="100%" styleName="caBot" >
   <mx:Panel title="播放列表" height="390" width="180"  x="10" y="10"  dropShadowEnabled="false">
    <mx:Tree dataProvider="{musicMenu}" labelField="@name"
     height="100%" width="150" id="tree" change="selectHandler(event)"/>
   </mx:Panel>
   
   <mx:DataGrid x="200" y="10" width="340" height="360" dataProvider="{playMenu.item}"
    id="playLs" doubleClick="newSong()" doubleClickEnabled="true">
    <mx:columns>
     <mx:DataGridColumn headerText="曲名" dataField="@name"/>
     <mx:DataGridColumn headerText="演唱者" dataField="@singer"/>
     <mx:DataGridColumn headerText="时间" dataField="@time"/>
     
    </mx:columns>
   </mx:DataGrid>
   
   <mx:Button x="199" y="376" label="清空列表" />
   <mx:TextInput x="346" y="376"/>
   
   <mx:TextArea x="550" y="10" height="390" width="190">
    
   </mx:TextArea>
   <mx:Label x="509" y="378" text="搜索"/>
   
  </mx:Canvas>
</mx:VBox>

</mx:Application>
 

不好意思有点长

这个是xml文件

<?xml version="1.0" encoding="utf-8"?>
<music name="album1">
 <item name="如果的事" sourse="mus/1.mp3" singer="张韶涵" time="0" />
 <item name="心雨" sourse="mus/2.mp3" singer="周杰伦" time="0"/>
 <item name="热带雨林" sourse="mus/3.mp3" singer="SHE" time="0" />
 <item name="贝壳" sourse="mus/4.mp3" singer="徐若瑄" time="0"/>
 <item name="单车之恋" sourse="mus/5.mp3" singer="马天宇" time="0"/>
 <item name="发现爱" sourse="mus/6.mp3" singer="李闰珉" time="0"/>
 <item name="穿越珊瑚海" sourse="mus/7.mp3" singer="牛奶咖啡" time="0"/>
 <item name="给我一首歌的时间" sourse="mus/8.mp3" singer="周杰伦" time="0"/>
 <item name="搁浅" sourse="mus/9.mp3" singer="马天宇" time="0"/>
 <item name="国境之南" sourse="mus/10.mp3" singer="范逸臣" time="0"/>
 <item name="花蝴蝶" sourse="mus/11.mp3" singer="蔡依林" time="0"/>
 <item name="水手" sourse="mus/12.mp3" singer="迪克牛仔" time="0"/>
 <item name="天使与海豚" sourse="mus/13.mp3" singer="梁咏琪" time="0"/>
 <item name="为爱痴狂" sourse="mus/14.mp3" singer="刘若英" time="0"/>
 <item name="小飞行" sourse="mus/15.mp3" singer="棉花糖" time="0"/>
 <item name="叶子" sourse="mus/16.mp3" singer="阿桑" time="0"/>
 <item name="左边" sourse="mus/17.mp3" singer="杨丞琳" time="0"/>
 <item name="锁住时间" sourse="mus/suozhushijian.mp3" singer="SHE" time="0"/>
 <item name="堪培拉的风" sourse="mus/kanpeiladefeng.mp3" singer="樊冲 张珊" time="0"/>
 <item name="Fallen Angel" sourse="mus/fallenangel.mp3" singer="后街男孩" time="0"/>
 <item name="With You" sourse="mus/withyou.mp3" singer="克里斯布朗" time="0"/>
 <item name="不敢当" sourse="mus/bugandang.mp3" singer="梁静茹" time="0"/>
 <item name="双人舞" sourse="mus/shuangrenwu.mp3" singer="潘玮柏" time="0"/>
 <item name="有你的快乐" sourse="mus/younidekuaile.mp3" singer="王若琳" time="0"/>
 <item name="把心拉近" sourse="mus/baxinlajin.mp3" singer="吴克群" time="0"/>
 <item name="听得见的梦想" sourse="mus/tingdejiandemengxiang.mp3" singer="张惠妹" time="0"/>
 <item name="爱极限" sourse="mus/aijixian.mp3" singer="许慧欣" time="0"/>
</music>

就着两个文件。