AS3 microphone recording/saving works, in-flash PCM playback double speed
- by Lowgain
I have a working mic recording script in AS3 which I have been able to successfully use to save .wav files to a server through AMF. These files playback fine in any audio player with no weird effects.
For reference, here is what I am doing to capture the mic's ByteArray: (within a class called AudioRecorder)
public function startRecording():void {
_rawData = new ByteArray();
_microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, _samplesCaptured, false, 0, true);
}
private function _samplesCaptured(e:SampleDataEvent):void {
_rawData.writeBytes(e.data);
}
This works with no problems. After the recording is complete I can take the _rawData variable and run it through a WavWriter class, etc.
However, if I run this same ByteArray as a sound using the following code which I adapted from the adobe cookbook: (within a class called WavPlayer)
public function playSound(data:ByteArray):void {
_wavData = data;
_wavData.position = 0;
_sound.addEventListener(SampleDataEvent.SAMPLE_DATA, _playSoundHandler);
_channel = _sound.play();
_channel.addEventListener(Event.SOUND_COMPLETE, _onPlaybackComplete, false, 0, true);
}
private function _playSoundHandler(e:SampleDataEvent):void {
if(_wavData.bytesAvailable <= 0) return;
for(var i:int = 0; i < 8192; i++) {
var sample:Number = 0;
if(_wavData.bytesAvailable > 0) sample = _wavData.readFloat();
e.data.writeFloat(sample);
}
}
The audio file plays at double speed! I checked recording bitrates and such and am pretty sure those are all correct, and I tried changing the buffer size and whatever other numbers I could think of. Could it be a mono vs stereo thing?
Hope I was clear enough here, thanks!