// you’re reading...

Actionscript 3.0

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.

Related Wuup Stuff:

You're Thoughts & Discussion

No comments for “AS3 Basics: Using the Microphone in Actionscript 3.0”

Post you're thoughts...