flex 纯客户端录音

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx"
      creationComplete="init()">
 
 <s:layout>
  <s:VerticalLayout/>
 </s:layout>
 
 <fx:Script>
  <![CDATA[
   import flash.events.SampleDataEvent;
   import flash.media.Microphone;
   import flash.media.Sound;
   import flash.utils.ByteArray;
   
   import mx.controls.Alert;
   import mx.utils.ObjectUtil;
   
   
   [Bindable] private var microphoneList:Array;
   protected var microphone:Microphone;
   protected var isRecording:Boolean = false;
   protected var soundRecording:ByteArray;
   protected var soundOutput:Sound;
   
   private var urlReq:URLRequest;
   private var sourceSnd:Sound;
   private var outputSnd:Sound;
   private var sChannel:SoundChannel;

   
   protected function init():void
   {
    
    
    urlReq= new URLRequest("a.mp3");  
    sourceSnd=new Sound();
    sourceSnd.load(urlReq);
    sourceSnd.addEventListener(Event.COMPLETE, function(e:Event):void{
     //outputSnd=new Sound();
     //outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
     //outputSnd.play();
     
        
     
     smr.enabled=true;
    });
    
     
    
    
    microphoneList = Microphone.names;
   }
   protected function processSound(event:SampleDataEvent):void
   {
    var bytes:ByteArray = new ByteArray();
    sourceSnd.extract(bytes, 8192); 
    
    while(bytes.bytesAvailable) {
     var sample:Number = bytes.readFloat();      
     //soundRecording.writeFloat(sample);     
    }
    
    
    event.data.writeBytes(bytes);
   }
   
   
   protected function setupMicrophone():void
   {
    microphone = Microphone.getMicrophone(comboMicList.selectedIndex);
    microphone.rate = 44;
    microphone.setUseEchoSuppression(true);
    microphone.setLoopBack(false);
    microphone.setSilenceLevel(0,0);
    microphone.gain=50;
    
    
   }
   
   protected function startMicRecording():void
   {
    
    
    
    //
    isRecording = true;
    soundRecording = new ByteArray();
    microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
    
    
    ///
    if(sChannel)
     sChannel.stop();
    outputSnd=new Sound();
    outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
    sChannel=outputSnd.play();
   }
   
   protected function stopMicRecording():void
   {
    isRecording = false;
    microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
    if(sChannel)
     sChannel.stop();
    outputSnd.removeEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
   }
   
   private function gotMicData(micData:SampleDataEvent):void
   {
       
    while(micData.data.bytesAvailable) {
     var sample:Number = micData.data.readFloat();      
     soundRecording.writeFloat(sample);
     
     
    }
    
   }
   
   protected function playbackData():void
    
   {
    soundRecording.position = 0;
    
    soundOutput = new Sound();
    soundOutput.addEventListener(SampleDataEvent.SAMPLE_DATA, playSound);
    
    soundOutput.play();
   }
   
   private function playSound(soundOutput:SampleDataEvent):void
   {
    //soundOutput.data.writeBytes(soundRecording);
    
    
    if (!soundRecording.bytesAvailable > 0)
     return;
    for (var i:int = 0; i < 8192; i++)
    {
     var sample:Number = 0;
     if (soundRecording.bytesAvailable > 0)
      sample = soundRecording.readFloat();
     soundOutput.data.writeFloat(sample);
     soundOutput.data.writeFloat(sample); 
     
    }
    
   }
   
   
  ]]>
 </fx:Script>
 
 <mx:ComboBox id="comboMicList" x="100" y="10" dataProvider="{microphoneList}"/>
 <s:Button width="161" label="Select Mic" click="setupMicrophone()"/>
 <s:Button id="smr" x="10" y="40" width="160" label="Start Rec" click="startMicRecording()"  enabled="false"/>
 <s:Button x="97" y="40" width="162" label="Stop Rec" click="stopMicRecording()"/>
 <s:Button x="11" y="70" width="162" label="Playback" click="playbackData()"/>
 
</s:Application>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值