论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

有关简单的flex mp3 player的开发

文章类别:Flash AS | 发表日期:2009-8-8 10:11:03

有关简单的flex mp3 player的开发

利用一些时间,整理的一个FLEX MP3 PLAYER,作品主要参考了《flex第一步》一书,相对比较简单。没有什么框架,或者分模块。主要解决了两个问题,一个问题是如果将文件信息通过XML文件加载到List组件当中,另外就是如何当选取List组件中的项目时,将音乐文件加载进来,并且侦听其声波和进度。
有些地方整理的不够细腻,甚至有错误,希望各位指正
MXML文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()" fontSize="12">
       <mx:Script>
              <![CDATA[
                            //事件相关类
                     import mx.events.ListEvent;
                     import mx.effects.Glow;
                     import mx.managers.CursorManager
                    
                     import flash.net.URLLoader
                     import flash.net.URLRequest
                     import flash.events.Event
                     import flash.events.SecurityErrorEvent
                     import flash.events.IOErrorEvent
                    
                     import mx.collections.ArrayCollection
                      //声音相关类
                     import flash.media.Sound
                     import flash.media.SoundChannel
                     import flash.media.SoundTransform
                     import flash.media.SoundMixer
                    
                     import flash.display.GradientType
                     import flash.filters.GlowFilter
                     import flash.utils.Timer
                    
                     import flash.events.TimerEvent
                     import mx.events.SliderEvent
                     //声音对象
                     private var sound:Sound
                     private var soundChannel:SoundChannel
                     //定义一个参数用于存放音乐的播放位置
                     private var position:Number=0
                     private var isPlaying:Boolean
                                               //定义一个定时器,用于定时纪录
                     private var soundTimer:Timer=new Timer(150,0)
                    
                 
             
                     private var xmlURL:String="XMLlist.xml"
                     private var loader:URLLoader
                    
                     //初始化函数
                     public function initApp():void{
                            //初始化数据
                            loader=new URLLoader()
                            var request:URLRequest=new URLRequest(xmlURL)
                            loader.addEventListener(Event.COMPLETE,completeHandler)
                            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler)
                            loader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler)
                            loader.load(request)
                            CursorManager.setBusyCursor()
                           
                            //曲目双击选择
                            mp3list.doubleClickEnabled=true
                            //曲目点击侦听
                            mp3list.addEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandler)
                            mp3list.addEventListener(MouseEvent.MOUSE_UP,showlist)
                                                                mp3list.addEventListener(MouseEvent.MOUSE_DOWN,stopSound)
                    
                                               btn_stop.enabled=false
                                      btn_play.enabled=false
                    
                                      slider_volume.value=1
                                      slider_pan.value=0      
                        
                         //音量控制
                         slider_volume.addEventListener(SliderEvent.CHANGE,changeVolume)
                            slider_pan.addEventListener(SliderEvent.CHANGE,changePan)
                    
                         //播放状态控制
                         btn_stop.addEventListener(MouseEvent.CLICK,stopSound)
                            btn_play.addEventListener(MouseEvent.CLICK,ppSound)
                        
                    
                         //声道状态侦听
                         soundChannel=new SoundChannel()
                                     soundChannel.addEventListener(Event.SOUND_COMPLETE,mp3EndHandler)
                                    }
                     
                      //XML数据初始化
                     internal function completeHandler(evt:Event):void{
                            CursorManager.removeBusyCursor()
                            var myXML:XML=XML(evt.target.data)
                            parseXML(myXML)
                     }
                     //异常处理
                     internal function securityErrorHandler(evt:SecurityErrorEvent):void{
                            trace("securityError:"+evt)
                     }
                     internal function ioErrorHandler(evt:IOErrorEvent):void{
                            trace("ioErrorEvent:"+evt)
                     }
                    
                     //解析XML数据
                     internal function parseXML(xml:XML):void{
                            var info:XML=xml
                            var items:ArrayCollection=new ArrayCollection()
                            for(var i:String in info..mp3){
                                   var obj:Object=new Object()
                                   var node:XML=info..mp3[i]
                                   obj.sing=node.sing
                                   obj.sanger=node.sanger
                                   obj.file=node.file
                                   obj.lyrics=node.lyrics
                                   items.addItem(obj)
                            }
                            mp3list.labelField="sing"
                            mp3list.dataProvider=items
                     }
                    
      
                                                //列出音乐内容
                     public function showlist(event:MouseEvent):void{
                           
                            var item:Object=mp3list.selectedItem
                                                      mp3_list.htmlText="<b>"+item.sing+"</b><br/>"
                                                      mp3_list.htmlText+="歌手:"+item.sanger+"<br/>"
                                                      mp3_list.htmlText+="<b>"+"歌词"+"</b>"+"<br/>"+item.lyrics+"<br/>"
                  
                                                     //音乐文件
                             var mp3URL:URLRequest=new URLRequest(item.file)
                    
                                     sound=new Sound()
                            sound.addEventListener(Event.COMPLETE,mp3completeHandler)
             
                                      sound.load(mp3URL)                      
                             //进度条侦听
                               progressbar.addEventListener(Event.ENTER_FRAME,progressHandler)
                        
                                              //音乐声波侦听
                               soundTimer.addEventListener(TimerEvent.TIMER,showSoundWave)

                                     var glow:GlowFilter=new GlowFilter()
                                    glow.color=0xAEB46
                                    glow.blurY=10
                                    glow.strength=2
                                    //发光滤镜
                             soundWave.filters=[glow]
                     }
                    


                            //音量控制
                         internal function changeVolume(evt:SliderEvent):void{
                               var soundControl:SoundTransform=soundChannel.soundTransform
                               soundControl.volume=evt.value
                               soundChannel.soundTransform=soundControl
                     }
                    
                            //声道控制
                         internal function changePan(evt:SliderEvent):void{
                                var soundControl:SoundTransform=soundChannel.soundTransform
                                soundControl.pan=evt.value
                                soundChannel.soundTransform=soundControl
                     }
                    
                     //音乐加载完成后的处理函数
                     internal function mp3completeHandler(evt:Event):void{
                                                                       btn_stop.enabled=false
                                   btn_play.enabled=true
                                   btn_play.label="play"
              }
                     //音乐播放完成后的处理函数
                     internal function mp3EndHandler(evt:Event):void{
                                btn_stop.enabled=true
        }
                    
                               //双击音乐播放函数
                     internal function doubleClickHandler(evt:MouseEvent):void{
                            if(isPlaying==false){
                              btn_stop.enabled=true
                              soundChannel=sound.play()
                              isPlaying=true
                              position=0
                              soundTimer.start()
                              btn_play.enabled=true
                              btn_play.label="pause"
                            }else{
                              btn_stop.enabled=true
                              soundChannel=sound.play()
                              isPlaying=true
                              position=0
                              soundTimer.start()
                              btn_play.enabled=true
                              btn_play.label="pause"
                            }
                     }
                    
      
                    
                     //进度条处理函数
                     internal function progressHandler(evt:Event):void{
                            var position:Number=soundChannel.position
                            var length:Number=sound.length
                            var percent:Number=Math.round(position*100/length)
                            progressbar.label=percent.toString()+"%"
                            progressbar.setProgress(position,length)
                     }
                    
                     //音乐暂停函数
                     internal function ppSound(evt:MouseEvent):void{
                            if(isPlaying){
                                   position=soundChannel.position
                                   soundChannel.stop()
                                   soundTimer.stop()
                                   btn_play.label="play"
                                   btn_stop.enabled=true
                            }else if(btn_stop.enabled==true){
                                   soundChannel=sound.play(position)
                                   soundTimer.start()
                                   btn_play.label="pause"
                                   btn_stop.enabled=true
                            }else{
                                   soundChannel=sound.play(0)
                                   soundTimer.start()
                                   btn_play.label="pause"
                                   btn_stop.enabled=true
                            }
                            isPlaying=!isPlaying
                     }
                    
                     //音乐停止函数
                     internal function stopSound(event:MouseEvent):void{
                                                          if(isPlaying){
                                                                  btn_stop.enabled=false
                                                                  soundTimer.stop()
                                             soundChannel.stop()
                                             btn_play.enabled=true
                                             btn_play.label="play"
                                             isPlaying=false
                                 
                                    }
                     }
                     //波形函数
                     internal function showSoundWave(evt:TimerEvent):void{
                            var g:Graphics=soundWave.graphics
                            g.clear()
                           
                            var soundData:ByteArray=new ByteArray()
                            SoundMixer.computeSpectrum(soundData,true,0)
                            g.lineStyle(1,0x006699,1)
                           
                            for(var i:Number=1;i<256;i+=1){
                                   var n:Number=soundData.readFloat()
                                   g.lineTo(i,-36*n)
                                   g.moveTo(i,-36*n)
                            }
                     }
              ]]>
       </mx:Script>
<mx:Panel x="10" y="10" width="400" height="220" layout="absolute" title="Mp3List" fontSize="12">
              <mx:HDividedBox width="100%" height="100%">
                     <mx:List id="mp3list" height="100%" width="110"/>
                     <mx:TextArea id="mp3_list" width="110" height="100%" editable="false"/>
              </mx:HDividedBox>
</mx:Panel>

<mx:Panel x="418" y="10" width="337" height="220" layout="absolute" title="Mp3Play">
              <mx:HSlider id="slider_volume" x="56" y="21" width="90" minimum="0" maximum="1" liveDragging="true"/>
              <mx:HSlider id="slider_pan" x="215" y="18" width="90" minimum="-1" maximum="1" liveDragging="true"/>
              <mx:Label x="3" y="19" text="volume"/>
              <mx:Label x="154" y="20" text="左右声道:"/>
             
<mx:Canvas x="6" y="44" width="301" height="100">
                     <mx:Canvas width="275" height="10" id="soundWave" y="51" x="20">
                     </mx:Canvas>
                     <mx:ProgressBar id="progressbar" width="301" height="15" y="69" labelPlacement="bottom" themeColor="#000D7A"
            minimum="0" visible="true" maximum="100" direction="right" mode="manual" barColor="0x669933" x="0"/>
</mx:Canvas>

<mx:ControlBar x="10" y="240">
              <mx:Spacer width="2"/>
              <mx:Button label="play" id="btn_play" width="65" height="20"/>
              <mx:Spacer/>
              <mx:Button label="stop" id="btn_stop" width="65" height="20"/>
       </mx:ControlBar>
</mx:Panel>
</mx:Application>

XML文件
<?xml version="1.0" encoding="utf-8"?>
<datas>
  <mp3>
     <sing>爱如潮水</sing>
     <sanger>张信哲</sanger>
     <file>mp3file/爱如潮水.mp3</file>
  </mp3>
    <mp3>
     <sing>流沙</sing>
     <sanger>陶吉吉</sanger>
     <file>mp3file/流沙.mp3</file>
  </mp3>
  <mp3>
     <sing>暖暖</sing>
     <sanger>梁静茹</sanger>
     <file>mp3file/暖暖.mp3</file>
  </mp3>
   <mp3>
     <sing>花田错</sing>
     <sanger>王力宏</sanger>
     <file>mp3file/花田错.mp3</file>
     <lyrics>夜好深了纸窗里怎么亮着</lyrics>
  </mp3>
   <mp3>
     <sing>宁夏</sing>
     <sanger>梁静茹</sanger>
     <file>mp3file/宁夏.mp3</file>
  </mp3>
   <mp3>
     <sing>少年游</sing>
     <sanger>王栎新</sanger>
     <file>mp3file/少年游.mp3</file>
  </mp3>
   <mp3>
     <sing>寂寞的季节</sing>
     <sanger>陶吉吉</sanger>
     <file>mp3file/寂寞的季节.mp3</file>
  </mp3>
</datas>

视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058