2012-12-10 53 views
0

你好我正在嘗試使用AS3 Worker錄製用戶聲音當涉及到工作人員和主場景之間的通信時,一切都很好,但我認爲錄製功能有問題並且在這裏發佈我正在使用Record從bytearray.org(http://www.bytearray.org/?p=1858) 這裏衝突的IS我的代碼,我失去的東西:使用AS3工作人員錄製聲音

package objects 
{ 
    import flash.events.Event; 
    import flash.system.MessageChannel; 
    import flash.system.Worker; 
    import flash.system.WorkerDomain; 
    import flash.utils.ByteArray; 

    import starling.display.Button; 
    import starling.display.Image; 
    import starling.display.Sprite; 
    import starling.events.Event; 

    public class RecordComponent extends Sprite 
    { 
     private var audioContainer:Image; 
     private var recButton:Button; 
     private var playButton:Button; 
     private var stopButton:Button; 

     //background thread for recording  
     private var worker:Worker; 
     private var wtm:MessageChannel; 
     private var mtw:MessageChannel; 
     private var output:ByteArray; 

     public function RecordComponent() 
     { 
      super(); 
      worker = WorkerDomain.current.createWorker(Workers.RecordWorker); 
      wtm = worker.createMessageChannel(Worker.current); 
      mtw = Worker.current.createMessageChannel(worker); 

      worker.setSharedProperty("wtm",wtm); 
      worker.setSharedProperty("mtw",mtw); 
      worker.start(); 
      wtm.addEventListener(flash.events.Event.CHANNEL_MESSAGE,onChannelMessage); 
      this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage); 
     } 

     protected function onChannelMessage(event:flash.events.Event):void 
     { 
      output = wtm.receive(); 
     } 

     protected function onAddedToStage(event:starling.events.Event):void 
     { 
      this.removeEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage); 
      drawScreen(); 
     } 

     private function drawScreen():void 
     { 
      audioContainer = new Image(Assets.getAtlas().getTexture("audioContainer")); 
      addChild(audioContainer);   

      //record button 
      recButton = new Button(Assets.getAtlas().getTexture("recordButton")); 
      recButton.x=Math.ceil(194/2 - recButton.width/2); 
      recButton.y=5; 
      addChild(recButton); 

      //stop button 
      stopButton = new Button(Assets.getAtlas().getTexture("stopButton")); 
      stopButton.x=recButton.x+10; 
      stopButton.y=10; 
      stopButton.visible = false; 
      addChild(stopButton); 

      //play button 
      playButton = new Button(Assets.getAtlas().getTexture("playButton")); 
      playButton.x=Math.ceil(194 - playButton.width)-25; 
      playButton.y=10; 
      playButton.visible = false; 
      addChild(playButton); 


      this.addEventListener(starling.events.Event.TRIGGERED, buttons_triggeredHandler); 
     } 

     private function buttons_triggeredHandler(event:starling.events.Event):void 
     { 
      var currentButton:Button = event.target as Button; 
      switch(currentButton) 
      { 
       case recButton: 
        startRecording(); 
        break; 
       case stopButton: 
        stopRecording(); 
        break; 
       case playButton: 
        playRecording(); 
        break; 
      } 
     } 

     private function playRecording():void 
     { 
      mtw.send("RECORD_PLAY"); 
     } 

     private function stopRecording():void 
     { 
      stopButton.visible = false; 
      recButton.x = 25; 
      playButton.visible = true; 
      recButton.visible = true; 
      mtw.send("RECORD_STOP"); 
      // 
     } 

     private function startRecording():void 
     { 
      recButton.visible = false; 
      stopButton.visible = true; 
      playButton.visible = false; 
      mtw.send("RECORD_START"); 
     } 



    } 
} 

RecordWorker

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.media.Microphone; 
    import flash.system.MessageChannel; 
    import flash.system.Worker; 

    import org.as3wavsound.WavSound; 
    import org.bytearray.micrecorder.MicRecorder; 
    import org.bytearray.micrecorder.encoder.WaveEncoder; 
    import org.bytearray.micrecorder.events.RecordingEvent; 

    public class RecordWorker extends Sprite 
    { 
     private var wtm:MessageChannel; 
     private var mtw:MessageChannel; 
     // volume in the final WAV file will be downsampled to 50% 
     private var volume:Number = .75; 
     // we create the WAV encoder to be used by MicRecorder 
     private var wavEncoder:WaveEncoder = new WaveEncoder(volume); 
     // we create the MicRecorder object which does the job 
     private var recorder:MicRecorder = new MicRecorder(wavEncoder,Microphone.getEnhancedMicrophone()); 

     public function RecordWorker() 
     { 
      wtm = Worker.current.getSharedProperty("wtm"); 
      mtw = Worker.current.getSharedProperty("mtw"); 

      mtw.addEventListener(Event.CHANNEL_MESSAGE,command_Handler); 
     } 
     //message received from the main component 
     protected function command_Handler(event:Event):void 
     { 
      switch(mtw.receive()) 
      { 
       case "RECORD_START": 
        trace("recording....."); 
        recorder.record(); 
        recorder.addEventListener(RecordingEvent.RECORDING, onRecording); 
        recorder.addEventListener(flash.events.Event.COMPLETE, onRecordComplete); 
        break; 
       case "RECORD_STOP": 
        trace("record stopped...."); 
        recorder.stop(); 
        break; 
       case "RECORD_PLAY": 
        if(recorder.output.length>0) 
        { 
         var player:WavSound = new WavSound(recorder.output); 
         player.play(); 
        } 
        break; 
      } 
     } 
     private function onRecording(event:RecordingEvent):void 
     { 
      trace (event.time); 
     } 

     private function onRecordComplete(event:flash.events.Event):void 
     { 

     } 
    } 
} 

ANNY幫助將可以訪問

感謝, 哈立德

回答

0

看着this write-up on Adobe's AS3 reference,似乎因爲每個創建的工作是「一個虛擬 Flash運行時的實例」,它有任何的核心運行時的輸入或輸出通道的沒有直接的聯繫。麥克風類是一個這樣的截止界面,這就是爲什麼它不能到達工作人員內部的系統麥克風。

+0

感謝您的回覆 –