0

如何从麦克风录制声音并从以下代码中保存

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
    <![CDATA[
        import flash.events.SampleDataEvent; 
        import flash.media.Microphone;
        import flash.net.FileReference;
        import mx.controls.Alert;
        import flash.net.FileReference;


        [Bindable] private var microphoneList:Array;
        protected var microphone:Microphone;
        protected var isRecording:Boolean = false;
        protected function setupMicrophoneList():void
        { 
            microphoneList = Microphone.names; 
        } 
        protected function setupMicrophone():void 
        {       
            microphone = Microphone.getMicrophone(comboMicList.selectedIndex);
        } 
        protected function startMicRecording():void 
        { 
            Alert.show("In recording");
            isRecording = true;
            Alert.show("In recording1");
            microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
            Alert.show("In recording22");
        }
        protected function stopMicRecording():void 
        { 
            isRecording = false;
            microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
        } 
        private function gotMicData(micData:SampleDataEvent):void 
        { 
            Alert.show("In mic data");
            // micData.data contains a ByteArray with our sample. }
            try{
            var file:FileReference = new FileReference();
            file.save(micData.data ,"Testsound.flv");
            }
            catch(e:Error)
            {
                Alert.show("In gotomicdataexception"+e);
            }
        }

        ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ComboBox x="150" id="comboMicList" dataProvider="{microphoneList}" />
<mx:Button x="250" id="startmicrec" label="Start Rec" click="startMicRecording()"/>
<mx:Button x="350" id="stopmicrec" label="Stop Rec" click="stopMicRecording()"/> 
<mx:Button x="50" id="setupmic" label="Select Mic" click="setupMicrophone()"/>
<mx:Button x="450" id="playrecsound" label="Play sound" click="playbackData()"/>

4

1 回答 1

0

这是您更新的代码,可让您在录制文件后保存文件。但无论如何,您需要将 bytearray 编码为 wav 或 mp3 等格式:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955" minHeight="600"
               creationComplete="this_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;


            [Bindable] private var microphoneList:Array;
            protected var microphone:Microphone;
            protected var isRecording:Boolean = false;
            private var soundBytes:ByteArray = new ByteArray();
            protected function setupMicrophoneList():void
            { 
                microphoneList = Microphone.names; 
            } 
            protected function setupMicrophone():void 
            {       
                microphone = Microphone.getMicrophone(comboMicList.selectedIndex);
            } 
            protected function startMicRecording():void 
            { 
                trace("In recording");
                isRecording = true;
                trace("In recording1");
                microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
                trace("In recording22");
            }
            protected function stopMicRecording():void 
            { 
                isRecording = false;
                microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
                saveMicData();
            } 

            private function saveMicData():void
            {
                trace("In mic data");
                // micData.data contains a ByteArray with our sample. }
                try{
                    var file:FileReference = new FileReference();

                    file.save(/*You need encoded soundBytes here*/soundBytes ,"Testsound.wav");
                }
                catch(e:Error)
                {
                    trace("In gotomicdataexception"+e);
                }
            }

            private function gotMicData(event:SampleDataEvent):void 
            { 
                while(event.data.bytesAvailable)
                {
                    var sample:Number = event.data.readFloat();
                    soundBytes.writeFloat(sample);
                }
            }

            private function playbackData():void
            {

            }

            protected function this_creationCompleteHandler(event:FlexEvent):void
            {
                setupMicrophoneList();
            }



        ]]>
    </fx:Script>
    <s:layout>
        <s:VerticalLayout />
    </s:layout>

    <mx:ComboBox id="comboMicList" dataProvider="{microphoneList}" />
    <mx:Button id="setupmic" label="Select Mic" click="setupMicrophone()"/>
    <s:HGroup>
        <mx:Button id="startmicrec" label="Start Rec" click="startMicRecording()"/>
        <mx:Button id="stopmicrec" label="Stop Rec" click="stopMicRecording()"/> 
        <!--<mx:Button id="playrecsound" label="Play sound" click="playbackData()"/>-->
    </s:HGroup>
</s:Application>
于 2012-04-24T11:46:41.027 回答