The Microphone

0
157

You can use the microphone in many applications on a mobile device. For example, you can take quick audio notes. You can add an audio caption to your photograph. You can even create a DJ multitrack tool with a convenient multitouch interface.

flash.media.Microphone is a static class for monitoring and capturing audio from the device’s native microphone. It is a subclass of the EventDispatcher class.

Your application needs the audio permission to use it:

<uses-permission android:name=”android.permission.RECORD_AUDIO”/>

First make sure your device has a microphone and gives you access to it:

import flash.media.Microphone;
if (Microphone.isSupported) {
// continue on
}

If it does, create a reference to it through its getMicrophone method. On Android devices, you only have access to one microphone:

var microphone:Microphone = Microphone.getMicrophone();

The microphone has several properties that you can set to improve your recording.

gain works as a volume multiplier and has a value between 0 and 100. The default value is 50 or a multiplier of 1. Any value above 50 boosts the microphone and below reduces it.

You want to boost the microphone to the maximum, not only because the mobile hardware is of a lesser quality than your desktop, but also because your users may be outdoors, or in a noisy place, while using your application:

microphone.gain = 100;

rate defines the sample rate and impacts the quality of the input audio. A higher sample rate gives a better sound quality but requires more memory on playback and takes more storage room. The sample rate is measured in kilohertz (kHz) with values of 5, 8, 11, 22, and 44. Considering that the microphone is only mono, a value of 22 should be sufficient. Try a smaller rate depending on your application’s needs:

microphone.rate = 22;

activityLevel is a read-only property that returns the amount of sound detected, from 0 to 100. It can be used as a visual aid for users to monitor how loud they should be speaking:

var level:Sprite = new Sprite();
level.x = stage.stageWidth*0.5;
level.y = stage.stageHeight*0.5;
level.graphics.beginFill(0xFF6600);
level.graphics.drawCircle(0, 0, 100);
level.graphics.endFill();
// while recording
level.width = microphone.activityLevel * 3;
level.height = microphone.activityLevel * 3;

silenceLevel represents the amount of noise needed to activate the microphone. The default is 10, but you can modify this using setSilenceLevel. A second optional parameter is the amount of time before a silent state starts:

microphone.setSilenceLevel(0, 3000);