用FLASH录制来至麦克风的声音(MicRecorder,一个微型麦克风类库)

MicRecorder.png
当我在写第二章 “字节可以做什么的时候?”,我想出了一个微型的帮助类,可以帮助开发者在他们的程序中轻松地从麦克风中录制音频。 

这就是MicRecorder,通过Flash Player 10.1中介绍的Microphone对象派发的Event.SAMPLE_DATA事件处理内部的ByteArray,并将WAV打包。 Wav解码器已被嵌入到其中,不过不久以后其他解码器也将可以被使用。

以下是一个小小的演示:
“http://bytearray.org/wp-content/projects/micrecorder/TestRecorder.swf
从应用程序中的录制麦克风音频,只需要以下几行代码:

  1. // volume in the final WAV file will be downsampled to 50%
  2. var volume:Number = .5;
  3. // we create the WAV encoder to be used by MicRecorder
  4. var wavEncoder:WaveEncoder = new WaveEncoder( volume );
  5. // we create the MicRecorder object which does the job
  6. var recorder:MicRecorder = new MicRecorder( wavEncoder );
  7. // starts recording
  8. recorder.record();
  9. // stop recording
  10. recorder.stop();

复制代码

当开始录制时,RecordingEvent.RECORDING事件被触发,并给出时间的信息。 当它停止录音时,Event.COMPLETE事件触发,你可以使用一个简单 的FileReference 对象将Micorder.output字节保存成音频流(作为一个wav):

  1. recorder.addEventListener(RecordingEvent.RECORDING, onRecording);
  2. recorder.addEventListener(Event.COMPLETE, onRecordComplete);
  3. private function onRecording(event:RecordingEvent):void
  4. {
  5. trace ( event.time );
  6. }
  7. private function onRecordComplete(event:Event):void
  8. {
  9. fileReference.save ( recorder.output, “recording.wav” );
  10. }

复制代码

您还可以重播录制好的WAV文件,通过使用非常优秀的as3wavsound库将原始的wav文件传递给WavSound对象:

  1. private function onRecordComplete(event:Event):void
  2. {
  3. var player:WavSound = new WavSound(recorder.output);
  4. player.play();
  5. }

复制代码

MicRecorder对象默认依赖于默认的麦克风设备,但在创建MicRecorder对象时,你可以更换Microphone实例:

  1. // a specific Microphone instance can be passed
  2. var recorder:MicRecorder = new MicRecorder( wavEncoder, microphoneDevice );

复制代码

您可以在这里下载到所有东西。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: