AS3 - 静音录音长度与录音不同

时间:2012-03-08 12:20:05

标签: actionscript-3 flash audio

我遇到了一个奇怪的问题,并想知道是否有人知道发生了什么。

使用ActionScript 3我使用麦克风录制13秒的声音片段。 我将mic.setSilenceLevel设置为0,因此数据一直在被采样。

单击开始以启动计时器并从麦克风写入字节数据。 13秒后计时器停止,我将数据写入byteArray,然后编码为mp3。

如果我录制13秒无声音,则mp3文件的长度为12.251秒。 如果我录制了13秒的声音,那么mp3文件的长度为12.512秒。

任何想法为什么?

mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);

private function micSampleDataHandler(event:SampleDataEvent):void
{   

var bytesRecorded:uint = soundBytes.length;
while( event.data.bytesAvailable )
{
    var sample:Number = event.data.readFloat();
    if( bytesRecorded < VOLUME_INC_BYTES )
    {
        // using linear dependence, but of course you can use a different one
        var volume:Number = bytesRecorded / VOLUME_INC_BYTES;
        soundBytes.writeFloat(sample * volume);
        bytesRecorded += 4;
    }else
    {
        soundBytes.writeFloat(sample);
    }
}
}


private function stopRecord():void {

mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);

var START_BYTES:uint = 0;

soundBytes.position = 0;

soundBA.clear();
soundBA.position = 0;

//insert the .2 seconds of silence to the new sound at the beginning

    for (var i:int = 0; i<4096; i++) { 
    soundBA.writeFloat(0); 
    soundBA.writeFloat(0); 
    } 

soundBA.writeBytes(soundBytes, START_BYTES);

soundBA.position = 0;


}

0 个答案:

没有答案
相关问题