Developing Android Applications with Flex 4.5 by Tretola Rich
Author:Tretola, Rich [Rich Tretola]
Language: eng
Format: epub
Tags: COMPUTERS / Hardware / Handheld Devices
ISBN: 9781449310134
Publisher: O'Reilly Media
Published: 2011-05-08T16:00:00+00:00
Figure 4-6. The Browse Gallery application
Figure 4-7. The Browse Gallery application with a picture selected
Microphone
If your application requires the use of the device’s microphone, you will need to select the RECORD_AUDIO permission when you are creating your project. See Chapter 3 for help with permissions.
Let’s review the code below. First, you will 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, 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 (instances of these variables will be created when the application runs).
There are three button components within the application to trigger the record, stop, and playback functionalities, respectively.
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 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. See the application in Figure 4-8:
<?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; privatevar microphone:Microphone; privatevar recording:ByteArray; privatevar sound:Sound; protectedfunction application1_applicationCompleteHandler (event:FlexEvent):void { if(Microphone.isSupported){ microphone = Microphone.getMicrophone(); microphone.rate = 44; microphone.setUseEchoSuppression(true); } else { status.text="Microphone NOT supported"; } } privatefunction micDataHandler(event:SampleDataEvent):void{ recording.writeBytes(event.data); } protectedfunction record_clickHandler(event:MouseEvent):void { recording = new ByteArray(); microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, micDataHandler); } protectedfunction stop_clickHandler(event:MouseEvent):void { microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, micDataHandler); } protectedfunction play_clickHandler(event:MouseEvent):void { recording.position = 0; sound = new Sound(); sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playSound); sound.play(); } privatefunction 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
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Circuits | Digital Design |
Electric Machinery & Motors | Electronics |
Fiber Optics | Networks |
Superconductivity |
Whiskies Galore by Ian Buxton(41493)
Introduction to Aircraft Design (Cambridge Aerospace Series) by John P. Fielding(32863)
Small Unmanned Fixed-wing Aircraft Design by Andrew J. Keane Andras Sobester James P. Scanlan & András Sóbester & James P. Scanlan(32536)
Craft Beer for the Homebrewer by Michael Agnew(17905)
Turbulence by E. J. Noyes(7665)
The Complete Stick Figure Physics Tutorials by Allen Sarah(7099)
Kaplan MCAT General Chemistry Review by Kaplan(6550)
The Thirst by Nesbo Jo(6409)
Bad Blood by John Carreyrou(6247)
Modelling of Convective Heat and Mass Transfer in Rotating Flows by Igor V. Shevchuk(6209)
Learning SQL by Alan Beaulieu(5997)
Weapons of Math Destruction by Cathy O'Neil(5781)
Man-made Catastrophes and Risk Information Concealment by Dmitry Chernov & Didier Sornette(5597)
Digital Minimalism by Cal Newport;(5328)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5152)
iGen by Jean M. Twenge(5129)
Secrets of Antigravity Propulsion: Tesla, UFOs, and Classified Aerospace Technology by Ph.D. Paul A. Laviolette(4867)
Design of Trajectory Optimization Approach for Space Maneuver Vehicle Skip Entry Problems by Runqi Chai & Al Savvaris & Antonios Tsourdos & Senchun Chai(4809)
Electronic Devices & Circuits by Jacob Millman & Christos C. Halkias(4706)
