flex 播放mp3

研究类flex 中的声音类,做了个播放mp3的工具呵呵,现在把源码贴出来。 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="OnClick()">
 <mx:Script>
  <![CDATA[
   import flash.media.*;
   import mx.events.SliderEvent ;
   private var _type:int=2;
        private var _sound:Sound;
        private var _channel:SoundChannel;
  private var posNum:Number=0;
        private var _spectrumGraph:BitmapData = new BitmapData(256, 60,
                                             true,
                                             0x000000aa);

//暂停
   private function OnPauseClick():void{
    posNum=_channel.position;
     _channel.stop();
   }

 

//停止

   private function OnStopClick():void{
    posNum=0;
     _channel.stop();
   }

   private function OnClick():void{
    hsldLeft.value=1;
   //var  spe:Spectrum=new Spectrum();
            // Create bitmap for spectrum display
   
 
           // pnlFrame.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            _sound = new Sound(new URLRequest("assets/song.mp3"));
            _channel = _sound.play( posNum );

  

   }

 //输出图形
         public function onEnterFrame():void
        {
         

           // Create the byte array and fill it with data
            var spectrum:ByteArray = new ByteArray(  );
            SoundMixer.computeSpectrum(spectrum);
           
            // Clear the bitmap
            _spectrumGraph.fillRect(_spectrumGraph.rect,
                                     0x00000000);

      
            // Create the left channel visualization
            var i:int;
           

           
           
            if(2==_type){
           for(i=0;i<256;i++) {
                 _spectrumGraph.setPixel32(i,
                               35 + spectrum.readFloat(  ) * 20,
                               0xffffffff);
             }
            }

            if(1==_type){
   
             for(i=0;i<64;i++) {
              _spectrumGraph.fillRect(new Rectangle(4*i,50-spectrum.readFloat(  ) * 50 ,4,spectrum.readFloat(  ) * 50 ),0xffffffff);
             }
            }
   cns.graphics.beginBitmapFill(_spectrumGraph);
   cns.graphics.drawRect(0,0,256,60);
   cns.graphics.endFill();   
   
   
   var curPos:Number=_channel.position;
   var curLen:Number=_sound.length;
   
   cns.graphics.beginFill(0x0);
   cns.graphics.drawRect(0,80,256,10);
   cns.graphics.endFill();

   cns.graphics.beginFill(0xa0);
   cns.graphics.drawRect(0,80,256*curPos/curLen,10);
   cns.graphics.endFill();

   
        }

//改变输出图形的

  private function OnCnsClick():void{
   _type--;
   if (0>=_type){
    _type=2;
   }
  }

 

//改变声音大小
  private function OnChangeVolumn( ):void{
       var transform:SoundTransform = _channel.soundTransform ;
   transform.volume =hsldLeft.value;
   _channel.soundTransform = transform;  
   
   trace("_channel.leftPeak="+_channel.leftPeak*100 +"   "+ "_channel.rightPeak="+_channel.rightPeak*100
    +"_channel.soundTransform.volume"+_channel.soundTransform.volume*100);   
  }
  
 
  
  ]]>
 </mx:Script>

 <mx:Canvas x="0" y="0" width="256" height="84" id="cns" enterFrame="onEnterFrame()" click="OnCnsClick()">
 </mx:Canvas>
 <mx:Button x="0" y="92" label="start" click="OnClick()"/>
 <mx:Button x="196" y="92" label="stop" click="OnStopClick()"/>
 <mx:Button x="96" y="92" label="pause" click="OnPauseClick()"/>
 <mx:HSlider x="10" y="122" height="12" width="237" minimum="0" maximum="2" id="hsldLeft"   change="OnChangeVolumn()" liveDragging="true"/>

 
</mx:Application>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值