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.

 


Posted

in

by

Tags: