开发者

Flash record and play plays 2x faster

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 = Microphon开发者_运维知识库e.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


I think you should not call this twice in your playbackSampleHandler.

    event.data.writeFloat(sample);
    event.data.writeFloat(sample);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜