我正在编写简单的AS3应用程序,它将从麦克风录制音频,然后使用ByteArray数据播放它。
我做了应用程序,它正在录制,但是当我尝试播放它时播放速度提高了2倍......
有趣的是,它之前没问题,现在它更快......而且即使我运行旧文件,其代码在100%工作之前也会更快...
这是代码:
import flash.media.*;
import flash.events.*;
import flash.utils.ByteArray;
var ch:SoundChannel;
var soundBytes:ByteArray = new ByteArray();
var soundO:ByteArray = new ByteArray();
var sound:Sound= new Sound();
var mic:Microphone = Microphone.getMicrophone();
var recMode:Boolean = false;
var playMode:Boolean = false;
function init()
{
mic.codec = "Speex";
mic.setSilenceLevel(0);
mic.gain = 50;
mic.rate = 44;
}
function startRecord():void
{
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
}
function stopRecord():void
{
mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
soundBytes.position = 0;
soundO.length = 0;
soundO.writeBytes(soundBytes);
soundO.position = 0;
soundBytes.length = 0;
}
function micSampleDataHandler(event:SampleDataEvent):void
{
while (event.data.bytesAvailable)
{
var sample:Number = event.data.readFloat();
soundBytes.writeFloat(sample);
}
}
function playSound():void
{
soundO.position = 0;
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
ch = sound.play();
ch.addEventListener(Event.SOUND_COMPLETE,onSC);
}
function stopSound():void
{
sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
ch.stop();
ch.removeEventListener(Event.SOUND_COMPLETE,onSC);
}
function onSC(evt:Event):void
{
stopSound();
soundO.position = 0;
playMode = ! true;
rec_btn.visible = true;
rec_btn2.visible = false;
play_btn.visible = true;
play_btn2.visible = false;
}
function playbackSampleHandler(event:SampleDataEvent):void
{
for (var i:int = 0; i < 8192; i++)
{
if (soundO.bytesAvailable < 4)
{
break;
}
var sample:Number = soundO.readFloat();
event.data.writeFloat(sample);
event.data.writeFloat(sample);
}
}
rec_btn.buttonMode = rec_btn2.buttonMode = play_btn.buttonMode = play_btn2.buttonMode = true;
function showPlayUI()
{
play_btn.visible = true;
}
function hidePlayUI()
{
play_btn2.visible = false;
rec_btn2.visible = false;
}
rec_btn.addEventListener(MouseEvent.CLICK,onRecord);
rec_btn2.addEventListener(MouseEvent.CLICK,onRecord);
play_btn.addEventListener(MouseEvent.CLICK,onPlay);
play_btn2.addEventListener(MouseEvent.CLICK,onPlay);
function onRecord(evt:MouseEvent=null):void
{
if (playMode)
{
playMode = ! true;
play_btn2.visible = false;
play_btn.visible = true;
stopSound();
}
if (! recMode)
{
recMode = true;
rec_btn.visible = false;
rec_btn2.visible = true;
play_btn2.visible = false;
startRecord();
}
else
{
recMode = ! true;
rec_btn.visible = true;
rec_btn2.visible = false;
play_btn2.visible = false;
stopRecord();
showPlayUI();
}
}
function onPlay(evt:MouseEvent=null):void
{
if (recMode)
{
recMode = false;
rec_btn.visible = true;
rec_btn2.visible = false;
stopRecord();
}
if (! playMode)
{
playMode = true;
rec_btn2.visible = false;
play_btn2.visible = true;
play_btn.visible = false;
playSound();
}
else
{
playMode = ! true;
rec_btn2.visible = false;
play_btn2.visible = false;
play_btn.visible = true;
stopSound();
}
}
hidePlayUI();
init();
正如您所见,编码率为44,即接受率...
我在Vista上测试了这个,在我的电脑上使用XP测试,并且两者都更快......
以下是网址: Record and Play
答案 0 :(得分:1)
我认为你不应该在你的playbackSampleHandler中调用两次。
event.data.writeFloat(sample);
event.data.writeFloat(sample);
答案 1 :(得分:0)
Speex编解码器存在问题,因为它对麦克风的编码方式不同,所以当你尝试将它的级别用作Float时,它会将其写成编码声音,而不是原始...
双线是可以的,它应该是这样......
ByteArray使用Speex编解码器无效,请使用默认编解码器。