0

您好,我正在尝试使用 AS3 Worker 录制用户声音,当涉及到 Worker 和主场景之间的通信时,一切正常,但我认为录制功能在这里有问题,我使用的是来自 bytearray.org 的 Record Clash(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");
        }



    }
}

唱片工作者

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 帮助将被应用

谢谢, 哈立德

4

1 回答 1

0

查看有关 Adob​​e 的 AS3 参考的这篇文章,似乎因为每个创建的工作人员都是“Flash 运行时的虚拟实例”,它与任何核心运行时的输入或输出通道没有直接连接。Microphone 类就是这样一种截断接口,这就是为什么它无法访问工作人员内部的系统麦克风的原因。

于 2013-08-21T07:45:51.467 回答