Skip to main content
July 21, 2012
Question

Capturing microphone sound data

  • July 21, 2012
  • 1 reply
  • 892 views

I tried to capture microphone sound data. so I searched in internet for someone to help me.

finally I find this address :

http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d1d.html

this address  belong Adobe help center

unfortunatelly  the  last  code  encounter  error  and  doesn't  work.

import flash.events.ActivityEvent;

import flash.events.StatusEvent;

import flash.media.Microphone;

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();

function micSampleDataHandler(event:SampleDataEvent):void

{

    while(event.data.bytesAvailable)

    {

        var sample:Number = event.data.readFloat();

        soundBytes.writeFloat(sample);

    }

}

var sound:Sound = new Sound();

var channel:SoundChannel;

function timerHandler(event:TimerEvent):void

{

    mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);

    timer.stop();

    soundBytes.position = 0;

    sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);

    channel.addEventListener( Event.SOUND_COMPLETE, playbackComplete );

    channel = sound.play();

}

function playbackSampleHandler(event:SampleDataEvent):void

{

    for (var i:int = 0; i < 8192 && soundBytes.bytesAvailable > 0; i++)

    {

        trace(sample);

        var sample:Number = soundBytes.readFloat();

        event.data.writeFloat(sample);

        event.data.writeFloat(sample);

    }

}

function playbackComplete( event:Event ):void

{

    trace( "Playback finished.");

}

error likes  this :

**Error** Scene 1, Layer 'Layer 1', Frame 1, Line 12: 1046: Type was not found or was not a compile-time constant: SampleDataEvent.

     function micSampleDataHandler(event:SampleDataEvent):void

**Error** Scene 1, Layer 'Layer 1', Frame 1, Line 32: 1046: Type was not found or was not a compile-time constant: SampleDataEvent.

     function playbackSampleHandler(event:SampleDataEvent):void

Total ActionScript Errors: 2,  Reported Errors: 2

    This topic has been closed for replies.

    1 reply

    July 23, 2012

    Hi,

    1. You need to import : import flash.events.SampleDataEvent;

    2. Also the target Flash Player version should be 10.0 or higher.

    Please check with these suggestions and let me know if it helps.

    Thanks,

    Apurva