2013-08-02 19 views
0

我得到這個代碼,用於記錄用戶的聲音:錄音與計時器事件

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"); 
} 
} 

我想,以取代計時器事件鼠標事件。如果持續時間== 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

如果您使用flash.utils.setTimeout()進行延遲呼叫,則可以做得更好。這使得所有與定時器的骯髒的工作給你。

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

The manual on setTimeout()

+0

維斯珀謝謝。我刪除了鼠標事件偵聽器並使用了您的建議。黃昏!我怎麼可以多次在特定的框架中調用這個類?可能嗎?因爲在其他幀中,我需要不同的時間延遲。例如,在第1幀中,錄製時間爲45秒,下一幀爲60秒。 – Amir

+0

嗯。您可以參數化初始化並在其他幀上手動寫入所需的時間。爲了多次調用一個函數,你仍然可以使用一個鼠標事件監聽器,它將包含一個'setTimeout()'調用或直接調用'startRecording()'和一個'setTimeout(stopRecording,<所需時間> )'電話。 – Vesper

+0

假設這是您的程序中的建築物中的一個街區。當然你可以從其他地方調用這個塊,並且可以將它變成一個函數,它將在幾秒鐘內抓住你的時間,轉換爲毫秒,並在所需的時間調用開始和停止記錄功能。 – Vesper