Skip to main content
Participant
January 17, 2009
Question

Mic audio capture and playback

  • January 17, 2009
  • 1 reply
  • 726 views
I've been developing in flash for a while now and now I have a job developing flash and actionscript 3.0. I have come across a project with a simple concept but I have found it difficult to implement.

What I want to do is allow the user to click a button and record their voice and then I want them to click another button and play it back. I don't need to save or store it and I don't have access to a flash media server or any alternative.

Is this possible to do and if so could someone please point me in the right direction.

Thank you
This topic has been closed for replies.

1 reply

Known Participant
January 1, 2011

Here you go.  I use this script so I know it works.  You need to have at least flash player 10.

You need 3 buttons.  1 record button - linkage = micRec , 1 play button - linkage = soundPlay  and one stop recording button - linkage = stopRecord

i keep the stop button off the stage and have it moved onto the stage when the record button is pushed.  Then finally move both the record and the stop recording button off the stage when the stop recording button is pushed.

micRec.addEventListener(MouseEvent.CLICK,recordMic);

function recordMic(event:MouseEvent){

micRec.x = -250;

stopRecord.x = 117;

const DELAY_LENGTH:int = 4000;

var mic:Microphone = Microphone.getMicrophone();

mic.setSilenceLevel(0, DELAY_LENGTH);

//mic.gain = 80;

mic.rate = 25;

mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);

stopRecord.addEventListener(MouseEvent.CLICK,SREC);

var soundBytes:ByteArray = new ByteArray();

function micSampleDataHandler(event:SampleDataEvent):void

{

    while(event.data.bytesAvailable)

    {

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

        soundBytes.writeFloat(sample);

    }

}

function SREC(event:MouseEvent):void

{      

stopRecord.x = -250;

    mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);

    soundBytes.position = 0;

    var sound:Sound = new Sound();

    sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);

soundPlay.addEventListener(MouseEvent.CLICK,SP1);

function SP1(event:MouseEvent){

soundBytes.position = 0;

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

    }

}

}

Participant
June 28, 2011

Had the same issue, thanks Flash Actions.

What is the scripting if I wanted to add a button to clear out the data that was recorded? When I record a second time, the playback plays both recorded items.

Thanks