Flash 录制和播放速度提高 2 倍
我正在制作简单的 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,这是接受率...
我在我的 PC 上的 Vista 和 XP 上测试了这个,并且在两者上都更快..这
是网址: 录音和播放
I am making simple AS3 application that will record audio from microphone and after that play it using ByteArray data.
I made application, it's recording, but when I try to play it it's playing 2x faster...
Interesting thing is that it was okay before, and now it's faster... And even when I run old file, that have code that was 100% working before it's also faster now...
Here is code:
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();
As you can see encode rate is 44, which is accept rate...
I tested this on Vista and on XP on my PC, and on both it's faster...
Here is the URL:
Record and Play
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你不应该在playbackSampleHandler 中调用它两次。
I think you should not call this twice in your playbackSampleHandler.
问题出在 Speex 编解码器中,因为它对麦克风的编码方式不同,所以当您尝试将其级别用作 Float 时,它会将其写入编码声音,而不是原始声音...
双线没问题,应该是这样...
ByteArray 无法与 Speex 编解码器一起正常工作,请改用默认编解码器。
Problem was in Speex codec, since it's encoding microphone differently, so when you try to use it's levels as Float it's writing it as encoded sound, not as original...
Double lines are okay, it should be that way...
ByteArray is not working okay with Speex codec, use default codec instead.