AS3 Basics: Using the Microphone in Actionscript 3.0

Flash CS3 Professional

While learning Actionscript I’ve found many interesting ways to add user interaction to a flash app. However, in my AS2 days, I missed using the microphone completely, so this is a chance for me to teach you as well as myself. Using the microphone you can add new ways for users to interact with you’re application.

Here is a basic example of using the microphone activity level to change the size of a movieclip.

Code:

var myMic:Microphone = Microphone.getMicrophone();
Security.showSettings(SecurityPanel.MICROPHONE);
myMic.setLoopBack(true);
myMic.setUseEchoSuppression(true);

stage.addEventListener(Event.ENTER_FRAME, stage_EnterFrame);
function stage_EnterFrame(e:Event)
{
line_mc.width = myMic.activityLevel*5;
activity_dyn.text = "Activity: " + String(myMic.activityLevel) + "%";
width_dyn.text = "Line_mc Width: " + String(line_mc.width) + "px";
trace(myMic.activityLevel);
}

Explanation:

Declare/Define the “myMic” varible, this will hold all the data to the microphone.

var myMic:Microphone = Microphone.getMicrophone();This shows the microphone panel so you can adjust you’re settings.

Security.showSettings(SecurityPanel.MICROPHONE);

Loops the captured audio through the speakers, if this is set to false capturing will stop. (no idea why).

myMic.setLoopBack(true);

This uses flash’s inbuilt method to reduce echo.

myMic.setUseEchoSuppression(true);

This starts our ENTER_FRAME event (so we run the function on entering every frame)

stage.addEventListener(Event.ENTER_FRAME, stage_EnterFrame);

Ah now this is the interesting bit, the update function. This will change our line_mc’s width property depending on the activity level of the microphone.

The activity level is an integer (whole number) from 0 – 100 (no volume/full volume).

function stage_EnterFrame(e:Event) { line_mc.width = myMic.activityLevel*5; activity_dyn.text = “Activity: ” + String(myMic.activityLevel) + “%”; width_dyn.text = “Line_mc Width: ” + String(line_mc.width) + “px”; trace(myMic.activityLevel); }

I have made up a commented FLA file of the demo above for you to download as i know most people will do that before reading the tutorial.

Tags: , , , , , , ,

  • http://www.trishladd.com Trish

    That’s great!

    I am a student at the Art Institute California at San Diego and currently I am looking to use the microphone to potentially develop a pitch trainer in flash for my Computer Based Training class.

    Do you know of a way to access the pitch of a sound coming from the microphone so that it can be compared to a range of values that represent the frequency of a note? The note musicians call Middle C has a frequency of 262Hz, would Flash have a way of detecting that?

  • http://www.bigtallbill.co.uk Bill Nunney

    Hi Trish thanks for the comment. As far as i’m aware the microphone is only capable of detecting activity level, its not capable of recording or anything else. However if you have an existing file (.mp3/.wav) flash is capable of getting far more detail.

    http://www.gotoandlearn.com/ have a great video tutorial on how to get at the data. Search for “Sound Spectrum Display”.

    Hope this helped!

    Peace Out
    Bill

  • giuliano

    Hi Bill,
    i’m really keen on mic interaction and on your article. Thank you for that!
    I can’t dowload the source. Can you help me please?

    Thank you,
    Giuliano