While working on the second chapter of "What can you do with bytes ?", I came up with a tiny helper class for developers who need to record audio easily from the microphone in their applications.
Here is MicRecorder, which handles internally the ByteArray work with the Event.SAMPLE_DATA event dispatched by the Microphone object introduced in Flash Player 10.1 and the WAV packaging. The WAV encoder is bundled but any other encoder could be used in a near future
Here is a little demo :
To record audio from the Microphone in your application, just use those few lines :
// volume in the final WAV file will be downsampled to 50% var volume:Number = .5; // we create the WAV encoder to be used by MicRecorder var wavEncoder:WaveEncoder = new WaveEncoder( volume ); // we create the MicRecorder object which does the job var recorder:MicRecorder = new MicRecorder( wavEncoder ); // starts recording recorder.record(); // stop recording recorder.stop();
When recording starts a RecordingEvent.RECORDING event is dispatched giving infos about time. When recording it stopped an Event.COMPLETE is dispatched allowing you to retrieve the Micorder.output bytes and save the audio stream (in this case as a WAV) using a simple FileReference object :
recorder.addEventListener(RecordingEvent.RECORDING, onRecording);
recorder.addEventListener(Event.COMPLETE, onRecordComplete);
private function onRecording(event:RecordingEvent):void
{
trace ( event.time );
}
private function onRecordComplete(event:Event):void
{
fileReference.save ( recorder.output, "recording.wav" );
}
You can also replay what has been recorded by passing the raw WAV file to the WavSound object from the nice as3wavsound library :
private function onRecordComplete(event:Event):void
{
var player:WavSound = new WavSound(recorder.output);
player.play();
}
The MicRecorder object relies by default on the default Microphone device available, but you can pass any Microphone instance as replacement when creating the MicRecorder object :
// a specific Microphone instance can be passed var recorder:MicRecorder = new MicRecorder( wavEncoder, microphoneDevice );
You can download everything here.
Comments (16)
Great lib. Thx.
Cool, thanks!
J’allais justement en faire une pour un projet sur lequel je travail, mais ça tombe à pic ! (Enfin, si le client accepte le fp 10.1..)
Cheers.
psychopathe
thx a lot!
Just what i was looking for ^_^
nice work… looking forward to chapter two
Amazing. How about encoding in mp3?
Great! as always
great article.
How to save in .mp3 ?????
Wow! another amazing lib.
Can’t wait the 2nd chapter!
Just what I need for an android app idea
Well, not quite everything I need, but I trust that a bundled MP3 encoder is in the works ^^
http://code.google.com/p/as3lameencoder/
Thibault, how about including not only one, but 2 encoders, so that we can choose between speed or quality?
http://code.google.com/p/flash-kikko/
Great work, thanks a lot!
Thibault, when will the 2nd chapter of your book be translated or published?
Salut Thibault, I’ve just tried your mic recorder but somehow it doesn’t work for me, the SampleDataEvent never seems to get called and in the end when I trace the byte array I get this value: RIFF. I’ve put in an ActivityEvent and it’s picking up the sound and the OnStatus event triggers as well but no luck recording…
I’ve noticed SampleData won’t work at run time testing using FDT3.5, I need to export as a final Air app build, it then works ; )
First of all Thanks Thibault for this, it will be pretty handful for me
But I’m dealing with the same problem as Immo, the SampleDataEvent doesn’t seem to fire. I’m using FDT 4 and compiling with Flex SDK4 for FP 10.1 and playing my swf at runtime with FP 10.1
The thing is that I have two projects using the MicRecorder :
- one of them is very simple and very similar to Thibault, here the sampleDataEvent is dispatched and my sound is recorded.
- The other one is a bit more complicated and the recorder is called in a specific module. And with the same configuration and instanciation of MicRecorder than the first project, the SampleDataEvent is dispatched.
So my question is : is there anything that could possibly block the event, a conflict with a Sound object or something like that ?
Please Help :’(
Trackbacks/Pingbacks (3)
[...] Direct Link [...]
[...] Thibault Imbert (@thibault_imbert) just came up with a really interesting helper class to easily record your microphone input via AS3. Thibault used the new Microphone Object which comes along with the Flash Player Version 10.1 – so having a look at the code is a good start to get in contact with it. You can check out everything on ByteArray.org. [...]
[...] MicRecorder, a tiny microphone library [ by Thibault Imbert ] [...]