3 Replies Latest reply on Nov 9, 2012 3:37 AM by kiran1989
      • 3. Re: flex 3 How to recording the voice to the local file.can u help sample souce code
        kiran1989 Level 2

        What? the link is not opening? Check once properly man.  If you need code here it is

         

        import flash.system.Security;
        import flash.media.Microphone;
        import flash.events.ActivityEvent;
        import flash.events.SampleDataEvent;
        import flash.events.StatusEvent;
        import flash.utils.ByteArray;
        import flash.media.Sound;
        import flash.events.MouseEvent;
        import flash.media.SoundChannel;
        import flash.events.Event;
        import org.bytearray.micrecorder.encoder.WaveEncoder;
        import flash.net.FileReference;

        var mic:Microphone;
        var soundBytes:ByteArray;
        var recBytes:ByteArray= new ByteArray();
        var sc:SoundChannel;
        var sound:Sound;
        recordBtn.addEventListener(MouseEvent.CLICK, getMic);
        stopBtn.addEventListener(MouseEvent.CLICK, stopRecording);
        playBtn.addEventListener(MouseEvent.CLICK, plays);
        saveBtn.addEventListener(MouseEvent.CLICK, saveAudio);

        function getMic(e:MouseEvent)
        {
        soundBytes= new ByteArray();
        //returns an array that total mics available
        var totalMics:Array = Microphone.names;
        //mic is a singleton class gets microphone to record sound
        mic = Microphone.getMicrophone();
        //Sets the minimum input level that should be considered
        mic.setSilenceLevel(0);
        mic.rate = 44;
        //to capture microphone audio listen for this event
        mic.addEventListener(SampleDataEvent.SAMPLE_DATA, recordAudio);

        }
        function recordAudio(e:SampleDataEvent)
        {
        //capture the byte array data available with the event
        while (e.data.bytesAvailable)
        {

        var soundData:Number = e.data.readFloat();
        soundBytes.writeFloat(soundData);
        }
        }
        function stopRecording(e:MouseEvent)
        {

        mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, recordAudio);
        //set the bytearray position to zero for playing from starting
        soundBytes.position = 0;
        }
        function plays(e:MouseEvent)
        {
        //intatiazle sound instance
        sound= new Sound();
        soundBytes.position = 0;
        //listen for the event when runtime requests new audio data.
        sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playAudio);
        //play the sound with sound channel
        sc = sound.play();
        sc.addEventListener(Event.SOUND_COMPLETE, soundComplete);
        }
        function playAudio(e:SampleDataEvent)
        {

        //if audio bytes is greate than 4 read the recorded byte array data and write it into the sound
        for (var i=0; i<8192; i++)
        {
        if (soundBytes.bytesAvailable > 4)
        {
        var sample:Number = soundBytes.readFloat();
        e.data.writeFloat(sample);
        e.data.writeFloat(sample);
        }
        }
        }

        function soundComplete(e:Event)
        {
        sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playAudio);
        sc.stop();
        }

        function saveAudio(e:MouseEvent)
        {
        soundBytes.position = 0;
        //waveencoder class used to encodes bytearray data properly used to play properly after saving
        //you can get this api from below link
        //http://code.google.com/p/micrecorder/downloads/detail?name=MicRecorder%201.2.zip&can=2&q=
        var encod:WaveEncoder= new WaveEncoder();
        var byte:ByteArray = encod.encode(soundBytes,2);
        var file:FileReference= new FileReference();
        file.save(byte, “test.wav”);

        }