When recording, the device buffers the microphone input. Create a ByteArray object to store the audio data. Create a reference to the microphone and add a listener to the SampleDataEvent.SAMPLE_DATA event:
import flash.media.Microphone;
import flash.utils.ByteArray;
import flash.events.SampleDataEvent;
var bytes:ByteArray = new ByteArray();var microphone:Microphone=Microphone.getMicrophone();
if (microphone!= null) {
microphone.gain = 100;
microphone.rate = 22;
microphone.setSilenceLevel(0, 4000);
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, recording);
}
function recording(event:SampleDataEvent):void {
while(event.data.bytesAvailable) {
var sample:Number = event.data.readFloat();
bytes.writeFloat(sample);
}
}
To stop the recording, remove the event listener. You can do this in code using a timer:
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, stopRecording);
timer.start();
function stopRecording(event:TimerEvent):void {
microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, recording);
timer.reset();
}
You can also let the user click a button to stop the recording. In this case, be aware of the memory and storage being used. You may want to prompt the user if the recording has been going on for more than a few seconds.