If your application requires the use of the device’s microphone,
you'll need to select the record_audio
and play_audio permissions when
creating your project.
Let’s review the following code. First, you'll notice that there is
a private variable named microphone declared of type
flash.media.Microphone. Within
applicationComplete of the application, an event
handler function is called; it first checks to see if the device supports
access to the microphone by reading the static property
of the Microphone class. If this property returns as
true, an instance of the Microphone is retrieved and
set to the microphone variable, the rate is set to 44,
and the setUseEchoSuppression method is used to set the
echo suppression to true. Variables of type ByteArray
and Sound are also declared within this application.
Instances of these variables will be created during use of this
application.
There are three button components within the
application to trigger the record, stop, and playback
functionalities.
Clicking the Record button will call the
record_clickHandler function, which will create a new
instance of the recording variable of type ByteArray.
An event listener of type SampleDataEvent.SAMPLE_DATA is added to the
microphone, which will call the micDataHandler method
when it receives data. Within the micDataHandler
method, the data is written to the recording
ByteArray.
Clicking the Stop button will stop the
recording by removing the SampleDataEvent.SAMPLE_DATA event
listener.
Clicking the Play button will call the
play_clickHandler method, which first sets the position
of the recording ByteArray to 0 so
it is ready for playback. It then creates a new instance of the
Sound class and sets it to the sound variable. It also
adds an event listener of type SampleDataEvent.SAMPLE_DATA that will call the
playSound method when it receives data. Finally, the
play method is called on the sound variable to start the playback.
The playSound method loops through the recording
ByteArray in memory and writes those bytes back to the
data property of the SampleDataEvent, which then plays
through the device’s speaker.
To extend this sample, you need to use some open source classes to
convert the recording ByteArray to an mp3 or wav file
so that it can be saved to disk. Figure 4-8
shows the application.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationComplete="application1_applicationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var microphone:Microphone;
private var recording:ByteArray;
private var sound:Sound;
protected function application1_applicationCompleteHandler
(event:FlexEvent):void
{
if(Microphone.isSupported){
microphone = Microphone.getMicrophone();
microphone.rate = 44;
microphone.setUseEchoSuppression(true);
} else {
status.text="Microphone NOT suported";
}
}
private function micDataHandler(event:SampleDataEvent):void{
recording.writeBytes(event.data);
}
protected function record_clickHandler(event:MouseEvent):void
{
recording = new ByteArray();
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA,
micDataHandler);
}
protected function stop_clickHandler(event:MouseEvent):void
{
microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA,
micDataHandler);
}
protected function play_clickHandler(event:MouseEvent):void
{
recording.position = 0;
sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playSound);
sound.play();
}
private function playSound(event:SampleDataEvent):void
{
for (var i:int = 0; i < 8192 && recording.bytesAvailable > 0; i++){
var sample:Number = recording.readFloat();
event.data.writeFloat(sample);
event.data.writeFloat(sample);
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label id="status"
text="Click Record to grab some audio, then Stop and Play it back"
top="10" width="100%" textAlign="center"/>
<s:HGroup top="80" horizontalCenter="0">
<s:Button id="record" label="Record" click="record_clickHandler(event)" />
<s:Button id="stop" label="Stop" click="stop_clickHandler(event)" />
<s:Button id="play" label="Play" click="play_clickHandler(event)" />
</s:HGroup>
</s:Application>