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 に答える