Microphone

All recently manufactured full sized iPods, iPhones, and iPads have microphones available.

Let’s review the code below. First, you will notice there is a private variable named microphone declared of type flash.media.Microphone. Within applicationComplete of the application an event handler function is called, which 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. There are also variables of type ByteArray and Sound declared within this application. There will be instances of these variables 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 would 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. The application can be seen in Figure 4-10:

<?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 supported";
                     }
               }

               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>
Microphone application

Figure 4-10. Microphone application

Get Developing iOS Applications with Flex 4.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.