I'm working on a project where the user have to record his/her voice, and submit it to server. But before submitting the user might need to play the recorded sound.
The application has a recording and playing capabilities with SPEEX codec. But what i found strange and difficult is when i the user plays back the recorded audio, the playing speed is faster or slower than normal that it cannot be understood. As if its fast forwarding.
Here is the sample code:
private var mic:Microphone;
private var rec:ByteArray;
private var snd:Sound;
private var channel:SoundChannel;
protected function recBtn_clickHandler(event:MouseEvent):void
{
rec = new ByteArray();
mic = Microphone.getMicrophone();
mic.setLoopBack(false);
mic.setUseEchoSuppression(true);
mic.gain = 50;
mic.setSilenceLevel(5, 1000);
mic.codec = SoundCodec.SPEEX;
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);
}
protected function plyBtn_clickHandler(event:MouseEvent):void
{
snd.addEventListener(SampleDataEvent.SAMPLE_DATA, playRecorded);
channel = snd.play();
}
private function getMicAudio(e:SampleDataEvent): void
{
rec.writeBytes(e.data);
}
private function playRecorded(e:SampleDataEvent): void
{
if (!rec.bytesAvailable > 0) return;
for (var i:int = 0; i < 2048; i++){
var sample:Number = 0;
if (rec.bytesAvailable > 0) sample = rec.readFloat();
for (var j:uint = 0; j < 6; j++) {
e.data.writeFloat(sample);
}
}
}
This scenario only happens when:
- mic.codec = SoundCodec.SPEEX;
- mic.rate = 16
I went through a lot of forums, but could not find any solution for Microphone playback with SPEEX codec or microphone.rate = 16;