使用AS3 Worker记录声音

时间:2012-12-10 15:55:06

标签: actionscript-3 flash flash-builder backgroundworker worker

您好我正在尝试使用AS3记录用户声音当工作者与主要场景之间的通信时,一切运行良好但我认为记录功能现在有问题,我在使用来自bytearray.org的记录冲突(http://www.bytearray.org/?p=1858) 这是我的代码,我错过了一些东西:

    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 help is appriciated

谢谢, 物语

1 个答案:

答案 0 :(得分:0)

查看this write-up on Adobe's AS3 reference,似乎因为每个创建的worker都是“Flash运行时的虚拟实例”,它与任何核心运行时的输入或输出都没有直接连接通道。 Microphone类是一个这样的截止接口,这就是为什么它无法到达工作人员内部的系统麦克风。