Question : Can I capture mic data?

I was reading help.adobe.com about sampleDataEvent and it says this about capturing microphone.

Capturing Microphone audio Use the sampleData event to capture audio data from a microphone. When you add an event listener for the sampleData event, the Microphone dispatches the event as audio samples become available.

Does this mean I can capture audio from a mic and play it back?
1:
2:
3:
4:
5:
6:
var soundBytes:ByteArray = new ByteArray();
 while(event.data.bytesAvailable)
 {
      var sample:Number = event.data.readFloat();
      soundBytes.writeFloat(sample);
 }

Answer : Can I capture mic data?

I guess it does.  Here is the code.  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
const DELAY_LENGTH:int = 4000;

var mic:Microphone = Microphone.getMicrophone();
mic.setSilenceLevel(0, DELAY_LENGTH);
mic.gain = 100;
mic.rate = 44;
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);

var timer:Timer = new Timer(DELAY_LENGTH);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

var soundBytes:ByteArray = new ByteArray();

function micSampleDataHandler(event:SampleDataEvent):void
{
    while(event.data.bytesAvailable)
    {
        var sample:Number = event.data.readFloat();
        soundBytes.writeFloat(sample);
    }
}

function timerHandler(event:TimerEvent):void
{        
    mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
    timer.stop();
    soundBytes.position = 0;
    var sound:Sound = new Sound();
    sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
    sound.play();
}

function playbackSampleHandler(event:SampleDataEvent):void
{
    for (var i:int = 0; i < 8192 && soundBytes.bytesAvailable > 0; i++) 
    {
        var sample:Number = soundBytes.readFloat();
        event.data.writeFloat(sample);
        event.data.writeFloat(sample);
    }
}
Random Solutions  
 
programming4us programming4us