带定时器事件的录音

时间:2013-08-02 07:17:08

标签: actionscript-3

我收到了用于录制用户声音的代码:

public class Main extends Sprite
     {
   private var mic:Microphone;
   private var waveEncoder:WaveEncoder = new WaveEncoder();
   private var recorder:MicRecorder = new MicRecorder(waveEncoder);
   private var recBar:RecBar = new RecBar();
   private var tween:Tween;
   private var fileReference:FileReference = new FileReference();

  public function Main():void
   {
    recButton.stop();
    activity.stop();
    mic = Microphone.getMicrophone();
    mic.setSilenceLevel(0);
    mic.gain = 100;
    mic.setLoopBack(true);
    mic.setUseEchoSuppression(true);
    Security.showSettings("2");
    addListeners();
}

private function addListeners():void
{
    recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
    recorder.addEventListener(RecordingEvent.RECORDING, recording);
    recorder.addEventListener(Event.COMPLETE, recordComplete);
    activity.addEventListener(Event.ENTER_FRAME, updateMeter);
}

private function startRecording(e:MouseEvent):void
{
    if (mic != null)
    {
        recorder.record();
        e.target.gotoAndStop(2);

        recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
        recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);

        addChild(recBar);

        tween = new Tween(recBar,"y",Strong.easeOut, -recBar.height,0,1,true);
    }
}

private function stopRecording(e:MouseEvent):void
{
    recorder.stop();

    mic.setLoopBack(false);
    e.target.gotoAndStop(1);

     recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
    recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);

    tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);
}

private function updateMeter(e:Event):void
{
    activity.gotoAndPlay(100 - mic.activityLevel);
}

private function recording(e:RecordingEvent):void
{
    var currentTime:int = Math.floor(e.time / 1000);

    recBar.counter.text = String(currentTime);

    if (String(currentTime).length == 1)
    {
        recBar.counter.text = "00:0" + currentTime;
    }
    else if (String(currentTime).length == 2)
    {
        recBar.counter.text = "00:" + currentTime;
    }
}

private function recordComplete(e:Event):void
{
    fileReference.save(recorder.output, "recording.wav");
}
}

我想用timer事件替换鼠标事件。如果持续时间== 5则开始录制,然后开始录制 10秒停止录音。我混淆了添加我的计时器代码的地方:

var myIntrotime:Timer = new Timer(1000,5);
myIntrotime.addEventListener(TimerEvent.TIMER, startIntroTime);
myIntrotime.start();

var SecondsElapsed:Number = 1;

function startIntroTime(event:TimerEvent):void
{
    if (SecondsElapsed==5)
    {
        //start recording
            //start another timer2 and if timer2 finished stop recording
    }
    SecondsElapsed++;
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

如果您使用flash.utils.setTimeout()拨打延迟电话,则可以做得更好。这使得所有肮脏的工作与定时器为您。

setTimeout(startIntroTime,5000)
function startIntroTime():void
{
    //start recording
    setTimeout(stopRecording,10000);
}

The manual on setTimeout()