参考以下代码。
import flash.events.SampleDataEvent;
import flash.media.Microphone;
import flash.media.Sound;
import flash.utils.ByteArray;
import flash.events.Event;
var microphone:Microphone;
var isPause:Boolean = false;
var soundRecording:ByteArray;
record_btn.addEventListener(MouseEvent.CLICK, onRecordStart);
recordPause_btn.addEventListener(MouseEvent.CLICK, onPauseResumeToggle);
recordStop_btn.addEventListener(MouseEvent.CLICK, onRecordStop);
audioPlay_btn.addEventListener(MouseEvent.CLICK, onPlay);
function onRecordStart(e:MouseEvent=null):void
{
soundRecording = new ByteArray();
microphone=Microphone.getMicrophone();
microphone.rate=44;
}
function onPauseResumeToggle(e:MouseEvent=null):void
{
if(!isPause)
{
microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}
else
{
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}
isPause != isPause;
}
function onRecordStop(e:MouseEvent=null):void
{
isPause = false;
microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
soundRecording.position=0;
}
function gotMicData(micData:SampleDataEvent):void
{
soundRecording.writeBytes(micData.data);
}
function onPlay(e:MouseEvent=null):void
{
soundRecording.position=0;
soundOutput = new Sound();
soundOutput.addEventListener(SampleDataEvent.SAMPLE_DATA, playSound);
soundOutput.play();
}
function playSound(soundOutput:SampleDataEvent):void
{
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);
}
}