Hi Guys,

All the posts in my blog are related to those topics which I was not knowing and I didn’t even find any good source from where I can get a good solution. I used to post all those issues here so that you may not face the same problem. I got totally frustrated with this issue as well as I found solutions for this but all of them were leading to recording with a heavy noise in them (which is of no profit to me). Finally I got a solution for this which is easy to use and records a clear voice and finally store them as an audio file.

The code is as follows:-

Make variables as follows:

var soundBytes:ByteArray;
var mic:Microphone;

On Start Event:-

soundBytes = new ByteArray();
mic = Microphone.getMicrophone();
mic.setLoopBack(false);
mic.encodeQuality = 100;
mic.setSilenceLevel( 0, 6000 );
mic.gain = 60;
mic.rate = 44;
mic.enableVAD=true;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.addEventListener(SampleDataEvent.SAMPLE_DATA,mic_sampleDataHandler);

Create a function mic_sampleDataHandler as follows:

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

On Stop Event :

mic.removeEventListener(SampleDataEvent.SAMPLE_DATA,mic_sampleDataHandler);
soundBytes.position = 0;
var wav:ByteArray = encodeToWav(soundBytes);
var appheader:URLRequestHeader = new URLRequestHeader(“Content-type”,”application/octet-stream”);
var jpgUrlRequest:URLRequest = new URLRequest(‘saveSound.php’);// saveSound.php is the file where you will write the code to save this audio file
jpgUrlRequest.requestHeaders.push( appheader );
jpgUrlRequest.method = “POST”;
jpgUrlRequest.data = wav;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener( Event.COMPLETE, handleSaveVoiceComplete );
urlLoader.load( jpgUrlRequest );

Function to encode the byte code into wav format

function encodeToWav( data:ByteArray ):ByteArray
{
var channels:uint = 2;
var BitsPerSample:uint = 16;
var SampleRate:uint = 44100;
var bytes: ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.writeUTFBytes( ‘RIFF’ );
bytes.writeInt( data.length );
bytes.writeUTFBytes( ‘WAVE’ );
bytes.writeUTFBytes( ‘fmt ‘ );
bytes.writeInt( 16 );
bytes.writeShort( 1 );
bytes.writeShort( channels );
bytes.writeInt( SampleRate );
bytes.writeInt( SampleRate * channels * BitsPerSample / 8 );
bytes.writeShort( channels * BitsPerSample / 8 ); bytes.writeShort( 16 );
bytes.writeUTFBytes( ‘data’ );
bytes.writeUnsignedInt( data.length );
bytes.writeBytes( soundBytes );
bytes.position = 0;
return bytes;
}

Write the following code in saveSound.php

<?php
if( isset($GLOBALS[‘HTTP_RAW_POST_DATA’]) )
{
$name = ‘voice_messages/’. date(‘Uu’). ‘.wav’;

$f = fopen( $name, ‘w+’);

if( fwrite( $f, $GLOBALS[‘HTTP_RAW_POST_DATA’] ) )
{
echo “path=”.$name;
}
else
{
echo 0;
}

fclose($f);
}
?>

Hopefully this will help you a lot!!!!!

Posted in: Uncategorized.
Last Modified: September 11, 2014

12 comments on “Audio Recording in flash using AS3

  1. boombeachcheats

    Hi to all, the contents present at this site are actually amazing for people knowledge, well, keep up the
    good work fellows.

    1. Kelvin

      This is a great tip. But I was caught more by the psd pesrar you mentioned. I wish I was more proficient with byte arrays. I’ve been looking for a starting point for writing/reading similar data structures. Specifically, compressing files and folders to simulate a file type. Do you know of any libraries or resources for writing and extracting compressed files? Thanks again

  2. mon ngon vung mien

    I have read so many posts on the topic of the blogger lovers however this paragraph is really a
    fastidious post, keep it up.

  3. cuisine

    I was curious if you ever thought of changing the structure of your site?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or 2 images.
    Maybe you could space it out better?

  4. Ronald

    Excellent pieces. Keep postng such kind of info on your page.
    Im really impressed by it.
    Hi there, You’ve performed a fantastic job.
    I will definitely digg it and in my opinion recommend to my friends.
    I am confident they will be benefited from this web site.

  5. Ask.fm

    I don’t even know how I ended up right here, however I believed
    this submit was great. I do not recognize who you
    are but definitely you are going to a well-known blogger inn the event you arre not already.

    Cheers!

  6. Scot

    Hi just wanted to gkve you a quick heads up and let you
    know a few of the images aren’t looading properly.
    I’m not sure why but I think its a linking issue.

    I’ve tried it in two different internet browsers
    and both show the same results.

    1. amanchhabra Post author

      Hi,

      Thanks for sharing. Hi I am unable to find any problematic image, it will be a great help if you can help me up and share exact images you are facing issue with.

      With Kind Regards,
      Aman Chhabra

  7. download Ask.fm tracker

    Good day I am so thrilled I found your webpage, I rreally found
    you by error, while I was looking onn Yahoo for something
    else, Nonetheless I aam here now annd would just like to say thank
    you for a fantastic post and a all round
    exciting blog (I also love the theme/design), I don’t have
    time to rezd it all at the moment but I have saved it and also
    added your RSS feeds, so wgen I have time I will
    be back to reead much more, Please do keep up the awesome work.

  8. download Ask.fm tracker

    With havin so much written content do you ever run into
    any problems of plagorism or copyright violation? My website has a lot of unique
    content I’ve either written myself or outsourced but it seeks a lott of it is popping it up all
    over the internet without my authorization. Do you know anny techniques to help redfuce content from being stolen? I’d genuinely appreciate it.

Leave a reply

required