·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[Flex]源代码实例mp3播放器(2)

时间:2014-06-03 14:10酷播
//方案二 ? xml version = 1.0 encoding = utf-8 ? mx:Application xmlns:mx = http://www.adobe.com/2006/mxml layout = absolute initialize = OnClick() mx:Script ![CDATA[ importflash.media.*; importmx.even

 

  1. // 方案二 
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="OnClick()"> 
  5.  <mx:Script> 
  6.   <![CDATA[ 
  7.    import flash.media.*; 
  8.    import mx.events.SliderEvent ; 
  9.    private var _type:int=2; 
  10.         private var _sound:Sound; 
  11.         private var _channel:SoundChannel; 
  12.   private var posNum:Number=0; 
  13.         private var _spectrumGraph:BitmapData = new BitmapData(256, 60, 
  14.                                              true, 
  15.                                              0x000000aa); 
  16.  
  17. //暂停 
  18.    private function OnPauseClick():void{ 
  19.     posNum=_channel.position; 
  20.      _channel.stop(); 
  21.    } 
  22.  
  23. //停止 
  24.  
  25.    private function OnStopClick():void{ 
  26.     posNum=0; 
  27.      _channel.stop(); 
  28.    } 
  29.  
  30.    private function OnClick():void{ 
  31.     hsldLeft.value=1; 
  32.    //var  spe:Spectrum=new Spectrum(); 
  33.             // Create bitmap for spectrum display 
  34.     
  35.   
  36.            // pnlFrame.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
  37.             _sound = new Sound(new URLRequest("assets/song.mp3")); 
  38.             _channel = _sound.play( posNum ); 
  39.  
  40.    
  41.  
  42.    } 
  43.  
  44.  //输出图形 
  45.          public function onEnterFrame():void 
  46.         { 
  47.           
  48.  
  49.            // Create the byte array and fill it with data 
  50.             var spectrum:ByteArray = new ByteArray(  ); 
  51.             SoundMixer.computeSpectrum(spectrum); 
  52.             
  53.             // Clear the bitmap 
  54.             _spectrumGraph.fillRect(_spectrumGraph.rect, 
  55.                                      0x00000000); 
  56.  
  57.        
  58.             // Create the left channel visualization 
  59.             var i:int; 
  60.             
  61.  
  62.             
  63.             
  64.             if(2==_type){ 
  65.            for(i=0;i<256;i++) { 
  66.                  _spectrumGraph.setPixel32(i, 
  67.                                35 + spectrum.readFloat(  ) * 20, 
  68.                                0xffffffff); 
  69.              } 
  70.             } 
  71.  
  72.             if(1==_type){ 
  73.     
  74.              for(i=0;i<64;i++) { 
  75.               _spectrumGraph.fillRect(new Rectangle(4*i,50-spectrum.readFloat(  ) * 50 ,4,spectrum.readFloat(  ) * 50 ),0xffffffff); 
  76.              } 
  77.             } 
  78.    cns.graphics.beginBitmapFill(_spectrumGraph); 
  79.    cns.graphics.drawRect(0,0,256,60); 
  80.    cns.graphics.endFill();    
  81.     
  82.     
  83.    var curPos:Number=_channel.position; 
  84.    var curLen:Number=_sound.length; 
  85.     
  86.    cns.graphics.beginFill(0x0); 
  87.    cns.graphics.drawRect(0,80,256,10); 
  88.    cns.graphics.endFill(); 
  89.  
  90.    cns.graphics.beginFill(0xa0); 
  91.    cns.graphics.drawRect(0,80,256*curPos/curLen,10); 
  92.    cns.graphics.endFill(); 
  93.  
  94.     
  95.         } 
  96.  
  97. //改变输出图形的 
  98.  
  99.   private function OnCnsClick():void{ 
  100.    _type--; 
  101.    if (0>=_type){ 
  102.     _type=2; 
  103.    } 
  104.   } 
  105.  
  106. //改变声音大小 
  107.   private function OnChangeVolumn( ):void{ 
  108.        var transform:SoundTransform = _channel.soundTransform ; 
  109.    transform.volume =hsldLeft.value; 
  110.    _channel.soundTransform = transform;   
  111.     
  112.    trace("_channel.leftPeak="+_channel.leftPeak*100 +"   "+ "_channel.rightPeak="+_channel.rightPeak*100 
  113.     +"_channel.soundTransform.volume"+_channel.soundTransform.volume*100);    
  114.   } 
  115.    
  116.   
  117.    
  118.   ]]> 
  119.  </mx:Script> 
  120.  
  121.  <mx:Canvas x="0" y="0" width="256" height="84" id="cns" enterFrame="onEnterFrame()" click="OnCnsClick()"> 
  122.  </mx:Canvas> 
  123.  <mx:Button x="0" y="92" label="start" click="OnClick()"/> 
  124.  <mx:Button x="196" y="92" label="stop" click="OnStopClick()"/> 
  125.  <mx:Button x="96" y="92" label="pause" click="OnPauseClick()"/> 
  126.  <mx:HSlider x="10" y="122" height="12" width="237" minimum="0" maximum="2" id="hsldLeft"   change="OnChangeVolumn()" liveDragging="true"/> 
  127.  
  128.   
  129. </mx:Application> 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器