[AS3] – MICROPHONE – RECORD AND PLAY

April 14, 2011 at 1:23 AM (Actionscript) (, , , )


Continuation to my previous post…here i’m trying out an simple record and play app:

click here for a DEMO
source files:
Link 1: microphone_recandplay1.zip – 13.0 KB
Link 2: microphone_recandplay1.zip – 13.0 KB


import flash.media.*;
import flash.events.*;
import flash.utils.ByteArray;


var ch:SoundChannel;
//ByteArray in which the recorded sound data is stored
var soundBytes:ByteArray = new ByteArray();
//ByteArray from which the recorded sound data is played
var soundO:ByteArray = new ByteArray();
//Sound object which plays the recorded sound...
var sound:Sound= new Sound();

//Gets your default microphone
var mic:Microphone = Microphone.getMicrophone();
//To check whether the application is recording the sound or not
var recMode:Boolean=false;
//To check whether the application is playing the sound or not
var playMode:Boolean=false;
//function called at start of application
function init(){
	//Sets the minimum input level that should be considered sound
	mic.setSilenceLevel(0);
	//The amount by which the microphone boosts the signal.
	mic.gain = 50;
	//The rate at which the microphone is capturing sound, in kHz.
	mic.rate = 44;
}
//function called when start Record button is clicked
function startRecord():void
{
	mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
}
//function called when stop Record button is clicked
function stopRecord():void
{
	mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
	soundBytes.position = 0;
	soundO.length=0
	soundO.writeBytes(soundBytes);
	soundO.position = 0;
	soundBytes.length=0;	
}

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

function playSound():void
{	
	sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
	ch=sound.play();
	ch.addEventListener(Event.SOUND_COMPLETE,onSC)	
}
function stopSound():void
{	
	sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
	ch.stop()
	ch.removeEventListener(Event.SOUND_COMPLETE,onSC)	
}
function onSC(evt:Event):void
{
	trace("SOUND_COMPLETE")
	stopSound()
	soundO.position=0
	playMode=!true
		play_lab.gotoAndStop(1)
		play_mesg.gotoAndStop(1)
		rec_btn.visible=rec_lab.visible=!false
}
function playbackSampleHandler(event:SampleDataEvent):void
{
	for (var i:int = 0; i < 8192; i++)
	{
		if (soundO.bytesAvailable < 4)
		{
			break
		}
		var sample:Number = soundO.readFloat();
		event.data.writeFloat(sample);
		event.data.writeFloat(sample);
		
		
	}
	if (soundO.bytesAvailable < 4&&soundO.position!==0)
	{
		
	}
}

rec_lab.mouseEnabled=play_lab.mouseEnabled=false
rec_btn.buttonMode=play_btn.buttonMode=true
rec_btn.addEventListener(MouseEvent.CLICK,onRecord)
play_btn.addEventListener(MouseEvent.CLICK,onPlay)

function showPlayUI(){
	play_btn.visible=play_lab.visible=true;
}
function hidePlayUI(){
	play_btn.visible=play_lab.visible=!true;
}

function onRecord(evt:MouseEvent=null):void
{
	if(!recMode){
		recMode=true
		rec_lab.gotoAndStop(2)
		rec_mesg.gotoAndPlay(2)
		startRecord()
	}else{
		recMode=!true
		rec_lab.gotoAndStop(1)
		rec_mesg.gotoAndStop(1)
		stopRecord()
		showPlayUI()
	}
}
function onPlay(evt:MouseEvent=null):void
{
	if(!playMode){
		playMode=true
		play_lab.gotoAndStop(2)
		play_mesg.gotoAndPlay(2)
		rec_btn.visible=rec_lab.visible=false
		playSound()
	}else{
		playMode=!true
		play_lab.gotoAndStop(1)
		play_mesg.gotoAndStop(1)
		rec_btn.visible=rec_lab.visible=!false
		stopSound()
	}
}
hidePlayUI()
init()
Advertisements

26 Comments

  1. Anonymous said,

    Thank you so much! The example in the adobe documentation does not work. Greetings from Germany!

  2. Geethanjali said,

    hello. Can anyone please help me.
    Actually i have created a voice recorder in as3(flash). it works fine in my local machine, but when i upload it to server. it gives all recordings in 1kb and 2 kb files. what might be the reason?. please help me.

  3. easypeasyinterface said,

    Hello! Thanks for the code and the tutorial, it works just fine! We tried to add a second frame, adding forth- and back buttons but after you jumped back to frame 1 the audio file isn’t “saved” anymore.
    I am looking for an opportunity to save the created “audio file” permanently so that it can be re-played after jumping to another frame and coming back. Plus, I’d like to have multiple record buttons in one swf to save multiple audios with the ability to play them over and over again. Is that possible? It would be a great help for us, thanks!

  4. Geethanjali said,

    Hi sathesh, everything seems to work fine now. thank you. Now i want to save the files to the server. can you please help.

    • sathesh said,

      I havn’t worked with red5 server, here is a link that I found might help you: http://tharas.wordpress.com/2009/12/20/upload-files-to-red5-server/

      • Anonymous said,

        Hi. Now i could rate the recorded audio voice to the server as well as to the local machine. I just have one doubt. I hope you could clear it. When i rate my files(swf) to the desktop. It seems to work fine . But when i rate the same files to any of the local disk. My swf file doesn’t work. It shows well in the flash application also. What might be the problem ?

      • geethanjali said,

        Hi. Now i could rate the recorded audio voice to the server as well as to the local machine. I just have one doubt. I hope you could clear it. When i rate my files(swf) to the desktop. It seems to work fine . But when i rate the same files to any of the local disk. My swf file doesn’t work. It shows well in the flash application also. What might be the problem ?

      • sathesh said,

        One reason i could think of is security issue, since your app communicates with a remote network, there might be a security issue, and have you added your desktop location as a trusted folder in your flash player global security settings(which might allow the file to work properly there)…check and confirm if your desktop folder is in trusted location…you can also check whats the issue by installing flash debug player, which will show a pop up window with error messages

      • Geethanjali said,

        You are right sathesh. Thanks a lot. Now i could play my swf files from anywhere.:)

      • geethanjali said,

        Hi Sathesh,
        I am with one other problem now. ican record and save the files to red5 server locally. now i want to upload the files to the server where my website is hosted. can you please help me in doing that. I think i have to install red 5 server on to the server where i hosted my website and upload the files to the server. can you please explain me the procedure to do that.

  5. Gethanjali said,

    Hi i couldn’t record the sound in my swf file. In flash application it is recording but when it comes to compiled swf, i couldn’t record and play. Please help.

  6. Gethanjali said,

    Hi i cannot record the sound in swf file. In flash i can record and play, but when it comes to swf, it is not recording as well as not playing. Please help

    • sathesh said,

      Hi,
      Are you able to run the demo, from the link provided?

      • Anonymous said,

        yes. When i copy the code into the flash application, it is recording. But when i publish swf and run it, its not showing the flash player settings to set the permission to allow. What might be the problem. Can you please explain me.

      • geethanjali said,

        yes. When i copy the code into the flash application, it is recording. But when i publish swf and run it, its not showing the flash player settings to set the permission to allow. What might be the problem. Can you please explain me.
        *

      • sathesh said,

        Are u using the same source code I have attached, or you have your own flash file, if you are working on a separate file, do send me your source files I’ll have a look into that, and let me try to figure out what’s causing your problem

      • Anonymous said,

        Yes i am using the same files with some modifications.I cant hear the recorded voice in your demo also. but when i download it and open in flash,i could record and hear it and when i play the same swf outside the flash i couldn’t record.

      • sathesh said,

        i tested my files again every thing seems to work fine, what flash ide/flashplayer version do you use, also send me your files ill check it here if it works here then we can zero in on the issue…have you tried it in any other system?

      • Anonymous said,

        I tried testing it on my laptop. It is working fine. but in my desktop, it is not. the only difference i found is i have flash player 10 installed on my laptop and flash player 11 on my desktop.

      • Anonymous said,

        Thank you. It is working fine now. Can you please help me in saving the recorded audio to the server. I have red 5 server installed on my machine. i want the client recorded voice to be saved in my server not in the local machine. Please help.

    • Anonymous said,

      *
      *
      * Anonymous on April 17, 2013 at 11:20 PM

      yes. When i copy the code into the flash application, it is recording. But when i publish swf and run it, its not showing the flash player settings to set the permission to allow. What might be the problem. Can you please explain me.
      *

  7. zimo said,

    Hi and thank u for sharing this tutorial.
    I’d like to know if – and how – is possible have the same result also with video.

    Thanks in advance,
    z

  8. yrussom said,

    Record And Play is just what I was looking for my educational apps. I like the demo.Where can I get the complete source code, including the GUI? I appreciate your response.

    • justflash said,

      hi, nice to hear my demo was of some use to you.. you can download the flash (cs5) file(source) from here: http://justflash.byethost12.com/

      • yrussom said,

        Hi
        Thank you for sending the download link. It is works very well, including the demo with “Save” button.

        One more question. How about adding “Open” button to open wave file and play. I am not sure how to do it.
        Thank you.
        yrussom

  9. [AS3] – MICROPHONE – RECORD AND SAVE AS WAVE FILE « JustFlash said,

    […] save feature to my previous demo Record and play, it saves the recorded sound to a wave […]

Leave a Reply

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 / Change )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: