Tuesday, October 26, 2010

Using the Microphone with the Flash Player to View Activity Levels

Flash Player Actionscript 3 Images
  1. Choose a microphone
  2. Click the close button.
  3. Allow access to activate microphone

Use the Microphone class to capture audio from a microphone attached to a computer running Adobe AIR. Use the Microphone class to monitor the audio locally. Use the NetConnection and NetStream classes to transmit the audio to Flash Media Server. Flash Media Server can send the audio to other servers and broadcast it to other clients running Adobe AIR.

A system might not have a microphone or other sound input device attached to it. You can use the Microphone.names property or the Microphone.getMicrophone() method to check whether the user has a sound input device installed. If the user doesn't have a sound input device installed, the names array has a length of zero, and the getMicrophone() method returns a value of null.

Calling the Microphone.getMicrophone() method without a parameter returns the first sound input device discovered on the user's system.

The Sound Player

  1. Create a dynamic text field with instance the name of activity_txt
  2. Create a dynamic text field with instance the name of width_txt.
  3. Create a dynamic text field with instance the name of status_txt
  4. Draw a line with instance the name of line_mc.

The Code

import flash.media.Microphone;

var myMic:Microphone = Microphone.getMicrophone();
Security.showSettings(SecurityPanel.MICROPHONE);
myMic.setLoopBack(true);
myMic.setUseEchoSuppression(true);
myMic.addEventListener(StatusEvent.STATUS, this.onMicStatus);

function onMicStatus(event:StatusEvent):void
{
if (event.code == "Microphone.Unmuted")
{
status_txt.text = "Microphone access was allowed.";
}
else if (event.code == "Microphone.Muted")
{
status_txt.text = "Microphone access was denied.";
}
}

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

  1. Calling the Microphone.setUseEchoSuppression() method with a parameter value of true reduces, but does not completely eliminate, the risk that audio feedback will occur.
  2. The activity level is an integer (whole number) from 0 – 100 (no volume/full volume).
  3. Test the SWF and if you have a microphone you will see the levels on the bar and text fields change.

ActionScript 3.0 Language and Components Reference Using the Microphone class

2 comments:

  1. hi,
    does it work with an android tablet?

    thanks

    ReplyDelete
  2. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics , i will be always checking your blog thanks.
    yeti review 2017

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...