Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [60]

By Root 2488 0
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);

Recording Audio


When recording, the device buffers the microphone input. Create a ByteArray object to store the audio data. Create a reference to the microphone and add a listener to the SampleDataEvent.SAMPLE_DATA event:

import flash.media.Microphone;

import flash.utils.ByteArray;

import flash.events.SampleDataEvent;

var bytes:ByteArray = new ByteArray();

var microphone:Microphone=Microphone.getMicrophone();

if (microphone!= null) {

microphone.gain = 100;

microphone.rate = 22;

microphone.setSilenceLevel(0, 4000);

microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, recording);

}

function recording(event:SampleDataEvent):void {

while(event.data.bytesAvailable) {

var sample:Number = event.data.readFloat();

bytes.writeFloat(sample);

}

}

To stop the recording, remove the event listener. You can do this in code using a timer:

import flash.utils.Timer;

import flash.events.TimerEvent;

var timer:Timer = new Timer(5000);

timer.addEventListener(TimerEvent.TIMER, stopRecording);

timer.start();

function stopRecording(event:TimerEvent):void {

microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, recording);

timer.reset();

}

You can also let the user click a button to stop the recording. In this case, be aware of the memory and storage being used. You may want to prompt the user if the recording has been going on for more than a few seconds.

Playing Back Audio


Create a sound object and a listener to read the samples stored in ByteArray. You must set the position on the first bytes to rewind to the beginning of the recording.

Audio data is made of samples; 8,192 samples is the maximum amount of data that can be written to a sound at one time. If you try to play more samples, you will get a runtime error. If you provide fewer than 2,048 samples, the application will assume the sound reached its end and will stop:

import flash.media.Sound;

if (bytes.length > 0) {

bytes.position = 0;

var sound:Sound = new Sound();

sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playback);

sound.play();

}

function playback(event:SampleDataEvent):void {

var sample:Number;

for (var i:int = 0; i < 8192; i++) {

if (bytes.bytesAvailable > 0) {

sample = bytes.readFloat();

event.data.writeFloat(sample); // channel left

event.data.writeFloat(sample); // channel right

}

}

}

Note that even though the microphone is monophonic in Flash, sound data needs to be written to both left and right output channels. If you don’t do this, the recording playback will be twice the speed at which it was recorded because two samples will run in one stereo sample of the sound object.

Saving a Recording


Let’s now save your recording on the device. In the following examples, we are saving the audio files on the SD card. Your application needs permission to write to external storage. If you do not have this permission, AIR will

Return Main Page Previous Page Next Page

®Online Book Reader