Friday, October 25, 2013

Flex 4.5 Audio Recorder :) Enjoy


<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow close="{PopUpManager.removePopUp(this)}" xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" cornerRadius="10" styleName="TitleWindow" width="300" height="200" title="Voice Recorder" creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<s:RemoteObject id="remotePat" 
showBusyCursor="true"
destination="patientSearchDestination"
fault="Alert.show('Remote Object Error')">
<s:method name="getAudioFileFromFolder"
 result="getAudioFileFromFolderResult(event)"/>

</s:RemoteObject>
</fx:Declarations>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.TitleWindow {
borderStyle: solid;
borderAlpha: 1;
roundedBottomCorners: true;
backgroundAlpha: 0.54;
highlightAlphas: 0.46, 0.28;
backgroundColor: #FFFFFF;
titleStyleName: "mypanelTitle";
}
.mypanelTitle {
color: #ffffff;
fontFamily: Arial;
fontSize: 12;
fontWeight: bold;
}
</fx:Style>
<fx:Script>
<![CDATA[
import com.aavanor.hospaa.utils.MessageUtil;
import com.aavanor.hospaa.doctor.view.script.WAVWriter;
import flash.events.Event;
import flash.events.SampleDataEvent;
import flash.media.Microphone;
import flash.media.Sound;
import flash.utils.ByteArray;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
private var soundBytes:ByteArray = new ByteArray();
private var mySound:Sound;
private var micTimer:Timer;
import fr.kikko.lab.ShineMP3Encoder;
import mx.controls.Alert;
private var mp3encoder:ShineMP3Encoder;
private var microphone:Microphone;
private var isRecording:Boolean=false;
private var soundRecording:ByteArray;

public var careId:Number;

public var patId:Number;

[Embed("/assets/images/microphoneNew.png")]
[Bindable]
public var microPhoneIcon:Class;


private function init():void {
// Get Microphone
stop_btn.enabled = false;
this.addEventListener(KeyboardEvent.KEY_DOWN, Esc);
rec_btn.setFocus();
microphone = Microphone.getMicrophone();
microphone.rate = 44;
microphone.setSilenceLevel(0);

microphone.setUseEchoSuppression(true);
microphone.soundTransform = new SoundTransform(0,0);
microphone.setLoopBack(true);

micTimer = new Timer(100);
micTimer.addEventListener(TimerEvent.TIMER, miclevel);
micTimer.start();
}
public function Esc(eve:KeyboardEvent):void
{
if (eve.keyCode == 27)
{
PopUpManager.removePopUp(this);
}
}

private function startMicRecording():void 
{
isRecording=true;
soundRecording = new ByteArray();
microphone=Microphone.getMicrophone();
microphone.rate=44;
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
rec_btn.enabled = false;
stop_btn.enabled=true;
}

private function stopMicRecording():void {

isRecording=false;
microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
soundRecording.position=0;
encodeToMP3(soundRecording);
rec_btn.enabled = true;
stop_btn.enabled=false;

}


private function gotMicData(micData:SampleDataEvent):void 
{
soundRecording.writeBytes(micData.data);
}

private function encodeToMP3(bytes:ByteArray) : void {
var wav:WAVWriter = new WAVWriter();
wav.numOfChannels = 1;
wav.sampleBitRate = 16;
wav.samplingRate =  44100;

bytes.position = 0;
var wavData : ByteArray = new ByteArray();
wavData.endian = Endian.BIG_ENDIAN;
wav.processSamples(wavData,bytes,44100,1);            
//wavData.position = 0;    
(new FileReference()).save(wavData, ".mp3");
// remotePat.getAudioFileFromFolder(wavData,careId,patId);
}

private function getAudioFileFromFolderResult(evt:ResultEvent):void
{
MessageUtil.showMessage("Voice Recorded Successfully");
PopUpManager.removePopUp(this);
}

/*
private function playbackSampleHandler(event:SampleDataEvent):void {
for (var i:int = 0; i < 8192 && soundRecording.bytesAvailable > 0; i++) {
var sample:Number = soundRecording.readFloat();
event.data.writeFloat(sample);
event.data.writeFloat(sample);
}

}

private function getAudioFileFromFolderResult(evt:ResultEvent):void
{
Alert.show("success");
}
private function doPlay(e:Event):void {

soundRecording.position = 0;
mySound = new Sound();
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
mySound.play();
}
*/
private function miclevel(event:TimerEvent):void {
myProgressBar.width = microphone.activityLevel * 2.5;
}

]]>
</fx:Script>
<s:VGroup height="90%" width="100%" horizontalAlign="center" verticalAlign="middle">
<!-- <s:Image source="{microPhoneIcon}" /> -->
<mx:HBox >

<s:Button label="Start" icon="@Embed('assets/images/microphoneRed.png')" id="rec_btn"  click="startMicRecording()"/>
<s:Button label="Stop" id="stop_btn"  icon="@Embed('assets/images/StopRecording.png')"  click="stopMicRecording()" />

</mx:HBox>
</s:VGroup>
<s:Group width="100%">
<s:Rect x="21" y="104" width="250" height="18" id = "myProgressBarFrame">
<s:stroke>
<s:SolidColorStroke color="0Xaaaaaa" weight="1"/>
</s:stroke>
</s:Rect>
<s:Rect x="23" y="106" width="250" height="15" id = "myProgressBar">
<s:fill>
<s:SolidColor color="green" id="myProgressBarFill"/>
</s:fill>
</s:Rect>
</s:Group>

<!-- <mx:HBox height="100%" width="100%" horizontalAlign="center" verticalAlign="middle">

<s:Image source="{microPhoneIcon}"/>
<s:Button x="17" y="66" label="Start Recording" id="rec_btn"  click="startMicRecording()"/>
<s:Button x="116" y="66" label="Stop Recording" id="stop_btn" click="stopMicRecording()" />
</mx:HBox>
<s:Rect x="21" y="104" width="250" height="18" id = "myProgressBarFrame">
<s:stroke>
<s:SolidColorStroke color="0Xaaaaaa" weight="1"/>
</s:stroke>
</s:Rect>
<s:Rect x="23" y="106" width="250" height="15" id = "myProgressBar">
<s:fill>
<s:SolidColor color="green" id="myProgressBarFill"/>
</s:fill>
</s:Rect>
-->
</s:TitleWindow>

No comments:

Post a Comment