AS3麦克风录音/保存工作,闪存内PCM播放双倍速
我在 AS3 中有一个有效的麦克风录音脚本,我已经能够成功地使用它通过 AMF 将 .wav 文件保存到服务器。这些文件在任何音频播放器中都可以正常播放,没有奇怪的效果。
作为参考,这是我正在做的捕获麦克风的 ByteArray 的操作:(在名为 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);
}
这可以正常工作。录音完成后,我可以获取 _rawData 变量并通过 WavWriter 类运行它,等等。
但是,如果我使用以下代码(我改编自 adobe Cookbook)将同一个 ByteArray 作为声音运行:(在名为 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);
}
}
音频文件以双倍速度播放!我检查了记录比特率等,并且非常确定这些都是正确的,并且我尝试更改缓冲区大小以及我能想到的任何其他数字。这可能是单声道与立体声的区别吗?
希望我在这里说得足够清楚,谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 ByteArray 必须包含两个通道(左通道和右通道)的数据,一个值紧接另一个值。因此,如果您的录音是单声道的,您的代码应该是这样的:
如果是立体声,您将需要进行相应的调整。
The problem is that the ByteArray has to contain data for both channels (left and right), one value immediately after the other. Thus, if your recording is mono, your code should be this:
If it is stereo, you will need to adjust accordingly.
我已经测试过。
您需要确保麦克风的速率为 44kHz:
_microphone.rate = 44;
这听起来应该是正确的。
我用过:
I've tested it.
You need to make sure the Microphone's rate is at 44kHz:
_microphone.rate = 44;
This should sound right.
I used: