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; publicclassSoundRecorder { publicfunctionSoundRecorder() { init(); } privatevarenco:WaveEncoder=newWaveEncoder(); privatevarfile:File; privatevarfs:FileStream = newFileStream(); privatevarch:SoundChannel; //ByteArray in which the recorded sound data is stored privatevarsoundBytes:ByteArray = newByteArray(); //ByteArray from which the recorded sound data is played privatevarsoundO:ByteArray = newByteArray(); //Sound object which plays the recorded sound... privatevarsound:Sound= newSound(); //Gets your default microphone privatevarmic:Microphone; //To check whether the application is recording the sound or not privatevarrecMode:Boolean=false; //To check whether the application is playing the sound or not privatevarplayMode:Boolean=false; //function called at start of application privatefunctioninit():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 privatefunctionstartRecord():void { mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); } //function called when stop Record button is clicked privatefunctionstopRecord():void { mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); soundBytes.position = 0; soundO.length=0 soundO.writeBytes(soundBytes); soundO.position = 0; soundBytes.length=0; } privatefunctionmicSampleDataHandler(event:SampleDataEvent):void { while (event.data.bytesAvailable) { varsample:Number = event.data.readFloat(); soundBytes.writeFloat(sample); } } privatefunctionplaySound():void { soundO.position = 0; sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler); ch=sound.play(); ch.addEventListener(Event.SOUND_COMPLETE,onSC) } privatefunctionstopSound():void { sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler); ch.stop() ch.removeEventListener(Event.SOUND_COMPLETE,onSC) } privatefunctiononSC(evt:Event):void { stopSound() soundO.position=0 playMode=!true } privatefunctionplaybackSampleHandler(event:SampleDataEvent):void { for (vari:int = 0; i < 8192; i++) { if (soundO.bytesAvailable < 4) { break } varsample:Number = soundO.readFloat(); event.data.writeFloat(sample); event.data.writeFloat(sample); } } publicfunctiononSave(fileName:String):void { soundO.position = 0 varo:ByteArray = enco.encode(soundO,2,16); file = File.applicationStorageDirectory.resolvePath("wav/"+fileName); fs.open(file,FileMode.WRITE); fs.writeBytes(o); fs.close();