2012-05-03 50 views
1

在下面的代码中,我试图将麦克风内容保存到文件中。保存的文件不播放,每次保存文件时,我都会看到只有10个字节的大小。我在代码中做了什么错误,可以给我看一个正确的代码来保存它。并且保存的文件应该相应地播放记录的内容。Adob​​e记录声音和保存

<?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; 
     import flash.display.Sprite; 
     import flash.media.Sound; 
     import flash.utils.ByteArray; 
     import flash.external.ExternalInterface; 




     public var _file:FileReference = new FileReference(); 
     [Bindable] private var micList:Array; 
     public var mic:Microphone = Microphone.getMicrophone(); 
     protected var isRecording:Boolean = false; 

     protected function startMicRecording():void 
     { 
      //var mic:Microphone = Microphone.getMicrophone(); 
      mic.gain = 60; 
      mic.rate = 11; 
      mic.setUseEchoSuppression(true); 
      mic.setLoopBack(true); 
      mic.setSilenceLevel(5, 1000); 
      Alert.show("In recording"); 
      isRecording = true; 
      mic.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData); 

     } 

     protected function stopMicRecording():void 
     { 


      //isRecording = false; 

      try{ 
      //_file.save(SampleDataEvent.SAMPLE_DATA, "recorded.wav"); 
       _file.save(SampleDataEvent.SAMPLE_DATA , "recorded.flv"); 
      } 
      catch(e:Error) 
      { 
       Alert.show("In Stopmicrecording"+e); 
      } 

     } 

     private function gotMicData(micData:SampleDataEvent):void 
     { 

      //mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData); 

     } 


     protected var soundRecording:ByteArray; 
     protected var soundOutput:Sound; 
     protected function playbackData():void 
     { 



     } 

     private function playSound(soundOutput:SampleDataEvent):void 
     { 

     } 







    ]]> 
</fx:Script> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<mx:ComboBox x="150" id="comboMicList" dataProvider="{micList}" /> 
<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()"/> 

</s:Application> 
+0

你第三次问(或许更多)几乎相同的问题。 – Art

+0

@Art:这是更具体的问题..因为我有一个解决方案.. – Rajeev

回答

1

你需要存储被交给你在gotMicDataByteArray,然后保存ByteArray数据。您正在保存事件名称,该名称是一个字符串(10个字符长)。

一旦你这样做,你需要加载文件和手中的样本数据的声音。您将声音播放8次...因为您以11 KHz采样但声音以44 KHz播放(4x写入),声音为立体声,但麦克风为单声道(再次为2x)。

您无法直接将数据保存为WAV文件...您记录了原始数据。如果您想要经历编写适当的WAV标题的麻烦,那么您不必再玩处理样本数据的游戏,并将文件交给Sound对象。这是一个超出了这个问题范围的练习。

祝你好运!

 import mx.controls.Alert; 

     public var mic:Microphone = Microphone.getMicrophone(); 
     public var recordedData:ByteArray; 

     protected function startMicRecording():void 
     { 
      mic.gain = 60; 
      mic.rate = 11; 
      mic.setUseEchoSuppression(true); 
      mic.setLoopBack(false); 
      mic.setSilenceLevel(5, 1000); 

      recordedData = new ByteArray(); 
      mic.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData); 

     } 

     protected function stopMicRecording():void 
     { 
      mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData); 

      try{ 
       var file:FileReference = new FileReference(); 
       file.save(recordedData, "recorded.dat"); 
      } 
      catch(e:Error) 
      { 
       Alert.show("In Stopmicrecording"+e); 
      } 

     } 

     private function gotMicData(sample:SampleDataEvent):void 
     { 
      recordedData.writeBytes(sample.data, 0, sample.data.bytesAvailable); 
     } 


     protected var playbackFile:FileReference; 
     protected var soundRecording:ByteArray; 
     protected var soundOutput:Sound; 
     protected function playbackData():void 
     { 
      playbackFile = new FileReference(); 
      playbackFile.addEventListener(Event.SELECT, playbackFileSelected); 
      playbackFile.browse(); 
     } 

     private function playbackFileSelected(event:Event):void { 
      playbackFile.addEventListener(Event.COMPLETE, playbackFileLoaded); 
      playbackFile.load(); 
     } 

     private function playbackFileLoaded(event:Event):void { 
      soundRecording = playbackFile.data; 
      soundOutput = new Sound(); 
      soundOutput.addEventListener(SampleDataEvent.SAMPLE_DATA, moreInput); 
      soundOutput.play(); 
     } 

     private function moreInput(event:SampleDataEvent):void { 
      var sample:Number; 
      for (var i:int = 0; i < 1024; i++) { 
       if (soundRecording.bytesAvailable > 0) { 
        sample = soundRecording.readFloat(); 

        // write the same byte 8 times: 
        // Upsample from 11 KHz to 44 KHz (x4) 
        // Mono to Stereo (x2) 
        for(var x:int = 0; x < 8; x++) 
         event.data.writeFloat(sample); 
       } 
      } 
     } 
+2

你也可以设置'mic.rate = 44'(以44KHz采样),然后当播放声音时,只写数据两次:'event.data.writeFloat(sample); event.data.writeFloat(sample);' –

+0

@SunilD。同意。在这种情况下,您的输出文件将增大4倍,但这只是成本/收益分析。 –

+0

一个问题..其中包括..event.data.writeFloat(sample); event.data.writeFloat(样品);虽然 – Rajeev