<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import flash.events.SampleDataEvent;
import flash.media.Microphone;
import flash.media.Sound;
import flash.utils.ByteArray;
import mx.controls.Alert;
import mx.utils.ObjectUtil;
[Bindable] private var microphoneList:Array;
protected var microphone:Microphone;
protected var isRecording:Boolean = false;
protected var soundRecording:ByteArray;
protected var soundOutput:Sound;
private var urlReq:URLRequest;
private var sourceSnd:Sound;
private var outputSnd:Sound;
private var sChannel:SoundChannel;
protected function init():void
{
urlReq= new URLRequest("a.mp3");
sourceSnd=new Sound();
sourceSnd.load(urlReq);
sourceSnd.addEventListener(Event.COMPLETE, function(e:Event):void{
//outputSnd=new Sound();
//outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
//outputSnd.play();
smr.enabled=true;
});
microphoneList = Microphone.names;
}
protected function processSound(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
sourceSnd.extract(bytes, 8192);
while(bytes.bytesAvailable) {
var sample:Number = bytes.readFloat();
//soundRecording.writeFloat(sample);
}
event.data.writeBytes(bytes);
}
protected function setupMicrophone():void
{
microphone = Microphone.getMicrophone(comboMicList.selectedIndex);
microphone.rate = 44;
microphone.setUseEchoSuppression(true);
microphone.setLoopBack(false);
microphone.setSilenceLevel(0,0);
microphone.gain=50;
}
protected function startMicRecording():void
{
//
isRecording = true;
soundRecording = new ByteArray();
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
///
if(sChannel)
sChannel.stop();
outputSnd=new Sound();
outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
sChannel=outputSnd.play();
}
protected function stopMicRecording():void
{
isRecording = false;
microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
if(sChannel)
sChannel.stop();
outputSnd.removeEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
}
private function gotMicData(micData:SampleDataEvent):void
{
while(micData.data.bytesAvailable) {
var sample:Number = micData.data.readFloat();
soundRecording.writeFloat(sample);
}
}
protected function playbackData():void
{
soundRecording.position = 0;
soundOutput = new Sound();
soundOutput.addEventListener(SampleDataEvent.SAMPLE_DATA, playSound);
soundOutput.play();
}
private function playSound(soundOutput:SampleDataEvent):void
{
//soundOutput.data.writeBytes(soundRecording);
if (!soundRecording.bytesAvailable > 0)
return;
for (var i:int = 0; i < 8192; i++)
{
var sample:Number = 0;
if (soundRecording.bytesAvailable > 0)
sample = soundRecording.readFloat();
soundOutput.data.writeFloat(sample);
soundOutput.data.writeFloat(sample);
}
}
]]>
</fx:Script>
<mx:ComboBox id="comboMicList" x="100" y="10" dataProvider="{microphoneList}"/>
<s:Button width="161" label="Select Mic" click="setupMicrophone()"/>
<s:Button id="smr" x="10" y="40" width="160" label="Start Rec" click="startMicRecording()" enabled="false"/>
<s:Button x="97" y="40" width="162" label="Stop Rec" click="stopMicRecording()"/>
<s:Button x="11" y="70" width="162" label="Playback" click="playbackData()"/>
</s:Application>