IT tutorials
 
Mobile
 

BlackBerry Tablet Applications : Exploring the APIs - Microphone

2/8/2013 11:11:09 AM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

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>

					  

Figure 1. Microphone application

 
Others
 
- Android : Getting Fancy with Lists - Inflating Rows Ourselves
- Android : Getting Fancy with Lists - A Dynamic Presentation
- Android : Getting Fancy with Lists - Getting to First Base
- Windows Phone 7 : Getting Started with XNA - Displaying Text
- Windows Phone 7 : Getting Started with XNA - Useful Sprite Effects
- iphone Programming : Integrating Your Application - Using the Address Book
- iphone Programming : Integrating Your Application - Media Playback
- Windows Phone 8 : Controls in XAML (part 2) - RichTextBox Control, Content Controls, List Controls
- Windows Phone 8 : Controls in XAML (part 1) - Using Keyboards
- Windows Phone 8 : XAML Overview - XAML Styling
 
25 Inspiring Game of Thrones Quotes
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS