[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

Permalink 26 Comments

« Previous page

%d bloggers like this: