Sound Recorder With Adobe Air

[![](http://tiger-a-s-s.tobybai.cn/43a39d58gw1e7lzg4d8i0g206y06ye81.gif)](http://tiger-a-s-s.tobybai.cn/43a39d58gw1e7lzg4d8i0g206y06ye81.gif)
图和文没有任何关系
由于我需要在我的mini项目中录制音频,所以我去找了一下Air录制音频的例子,发现很早以前就已经有人在讨论这个问题了,地址如下。
[Saving Microphone Recording IOS/Android](http://forums.adobe.com/message/3721365)我担心的是,这些问题是大约2年前被讨论的,里面涉及到的工具包和代码库是否仍然能在iOS7下面运行呢,答案是,YES!他们一样能正常工作,我的实现方案分成如下步骤,录音,保存成WAV,然后回放。其中录音和保存输出的部分,在这个开源项目中都已经达成了,[micrecorder](https://code.google.com/p/micrecorder/),我又找到了一个忘记什么时候找到的兄台封装的代码,如下:
 package utils  
 {  
      import flash.events.Event;  
      import flash.events.SampleDataEvent;  
      import flash.filesystem.File;  
      import flash.filesystem.FileMode;  
      import flash.filesystem.FileStream;  
      import flash.media.Microphone;  
      import flash.media.Sound;  
      import flash.media.SoundChannel;  
      import flash.utils.ByteArray;  
      import org.bytearray.micrecorder.encoder.WaveEncoder;  
      public class SoundRecorder  
      {  
           public function SoundRecorder()  
           {  
                init();  
           }  
           private var enco:WaveEncoder=new WaveEncoder();  
           private var file:File;  
           private var fs:FileStream = new FileStream();  
           private var ch:SoundChannel;  
           //ByteArray in which the recorded sound data is stored  
           private var soundBytes:ByteArray = new ByteArray();  
           //ByteArray from which the recorded sound data is played  
           private var soundO:ByteArray = new ByteArray();  
           //Sound object which plays the recorded sound...  
           private var sound:Sound= new Sound();  
           //Gets your default microphone  
           private var mic:Microphone;  
           //To check whether the application is recording the sound or not  
           private var recMode:Boolean=false;  
           //To check whether the application is playing the sound or not  
           private var playMode:Boolean=false;  
           //function called at start of application  
           private function init():void{  
                mic = Microphone.getMicrophone();  
                //Sets the minimum input level that should be considered sound  
                mic.setSilenceLevel(0);  
                //The amount by which the microphone boosts the signal.  
                mic.gain = 100;  
                //The rate at which the microphone is capturing sound, in kHz.  
                mic.rate = 44;  
           }  
           //function called when start Record button is clicked  
           private function startRecord():void  
           {  
                mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);  
           }  
           //function called when stop Record button is clicked  
           private function stopRecord():void  
           {  
                mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);  
                soundBytes.position = 0;  
                soundO.length=0  
                soundO.writeBytes(soundBytes);  
                soundO.position = 0;  
                soundBytes.length=0;    
           }  
           private function micSampleDataHandler(event:SampleDataEvent):void  
           {  
                while (event.data.bytesAvailable)  
                {  
                     var sample:Number = event.data.readFloat();  
                     soundBytes.writeFloat(sample);  
                }  
           }  
           private function playSound():void  
           {    
                soundO.position = 0;  
                sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);  
                ch=sound.play();  
                ch.addEventListener(Event.SOUND_COMPLETE,onSC)   
           }  
           private function stopSound():void  
           {    
                sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);  
                ch.stop()  
                ch.removeEventListener(Event.SOUND_COMPLETE,onSC)    
           }  
           private function onSC(evt:Event):void  
           {  
                stopSound()  
                soundO.position=0  
                playMode=!true  
           }  
           private function playbackSampleHandler(event:SampleDataEvent):void  
           {  
                for (var i:int = 0; i < 8192; i++)  
                {  
                     if (soundO.bytesAvailable < 4)  
                     {  
                          break  
                     }  
                     var sample:Number = soundO.readFloat();  
                     event.data.writeFloat(sample);  
                     event.data.writeFloat(sample);  
                }  
           }  
           public function onSave(fileName:String):void  
           {  
                soundO.position = 0  
                var o:ByteArray = enco.encode(soundO,2,16);  
                file = File.applicationStorageDirectory.resolvePath("wav/"+fileName);  
                fs.open(file,FileMode.WRITE);  
                fs.writeBytes(o);  
                fs.close();  
 //               file.save(o,fileName);  
           }  
           public function onRecord():void  
           {  
                if(!recMode){  
                     recMode=true  
                     startRecord()  
                }else{  
                     recMode = !true;  
                     stopRecord();  
                }  
           }  
           public function onPlay():void  
           {  
                if(!playMode){  
                     playMode=true  
                     playSound()  
                }else{  
                     playMode=!true;  
                     stopSound();  
                }  
           }  
      }  
 }  
`</pre>
<span>这里需要注意的是,不要在程序启动瞬间马上初始化它,</span>
<span>否则 mic = Microphone.getMicrophone();会失效,录制不到任何声音。</span>
<span>
</span>之后遇到的问题就是回放WAV格式的文件,利用了这里的库,[as3wavsound](https://code.google.com/p/as3wavsound/),回放的代码如下:
<pre>` var bytes:ByteArray = new ByteArray();  
 var inputFile:File = File.applicationStorageDirectory.resolvePath("wav/b.wav") ;  
 var inputStream:FileStream = new FileStream();  
 inputStream.open(inputFile,FileMode.READ);  
 inputStream.readBytes(bytes,0,inputStream.bytesAvailable);  
 inputStream.close();  
 var wavPlayer:WavSound = new WavSound(bytes);  
 wavPlayer.play(0,0);