0 Replies Latest reply on Sep 30, 2009 4:16 PM by KronInt

    Audio recording in flex

    KronInt

      I don't know much about flex but i wrote a code that should record a voice...

      Can someone tell why this code doesn't work?

       

      This is code

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

      xmlns:mx="http://www.adobe.com/2006/mxml"

      layout="absolute"

      width="323"

      height="150"

      backgroundGradientAlphas="[1.0, 1.0]"

      backgroundGradientColors="[#f4f4f4, #cccccc]"

      creationComplete="init()">

      <mx:Style>

      .playStyle {icon: Embed(source="icons/play_grey.png");}

      .pauseStyle {icon: Embed(source="icons/pause_grey.png");}

      </mx:Style>

        

        

      <mx:Script>

      <![CDATA[

      import mx.controls.Alert;

      import flash.net.NetStream;

      private var myMic:Microphone;

      private var nc:NetConnection;

      private var ns:NetStream;

      private var recordingState:String = "idle";

      private var myDuration:Number = 0;

      private var myServer:String = null;

      private var mySoundFile:String = null;

      private var myTimer:Timer = null;

      private var playbackTimer:Timer = null;

      private var timeLimit:Number = 0;

      private function init():void {

      // Not yet connected to the server. Disable the buttons.

      record_btn.enabled = false;

      stop_btn.enabled = false;

      play_btn.enabled = false;

      myServer = Application.application.parameters.myServer;

      mySoundFile = Application.application.parameters.mySound;

      timeLimit = Application.application.parameters.timeLimit;

       

      initMic("0xcccccc");

       

      myTimer = new Timer(1000, timeLimit);

      myTimer.addEventListener("timer", timerHandler);

      timeText.text = "Recording time limit: " + createCountdown(0);

      record_btn.addEventListener(MouseEvent.CLICK, record_start);

      stop_btn.addEventListener(MouseEvent.CLICK, record_stop);

      play_btn.addEventListener(MouseEvent.CLICK, record_play);

       

      setupNC();

      addEventListener(Event.ENTER_FRAME, showMicLevel);

      myMic = Microphone.getMicrophone();

      myMic.setSilenceLevel(0);

      myMic.rate = 22;

      statusText.text = "connecting";

      }

       

      private function setupNC():void{

      nc = new NetConnection();

      nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

      nc.connect(myServer);

      }

       

      private function netStatus(event:NetStatusEvent):void {

      switch (event.info.code){

      case "NetConnection.Connect.Success" :

      setStatus("Connected");

      ns = new NetStream(nc);

      ns.attachAudio(myMic);

      record_btn.enabled = true;

      stop_btn.enabled = true;

      play_btn.enabled = true;

      break;

      case "NetStream.Record.Start" :

      setStatus("recording...");

      break;

      case "NetStream.Record.Stop" :

      setStatus("stopped recording");

      break;

      case "NetStream.Buffer.Full" :

      setStatus("playing");

      break;

      case "NetStream.Play.Start" :

      setStatus ("buffering");

      break;

      case "NetStream.Seek.Notify" :

      setStatus("Seeking");

      break;

      case "NetStream.Play.Reset" :

      setStatus("pausing");

      break;

      case "NetStream.Play.Stop" :

      //setStatus("stopping");

      break;

      case "NetStream.Buffer.Flush" :

      //setStatus("flushing buffer");

      break;

      case "NetStream.Play.Complete" :

      setStatus("Playback complete");

      break;

      }

      }

       

      private function record_start(event:Event):void{

      initMic("0xff0000");

      ns.close();

      ns = new NetStream(nc);

      ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

      ns.attachAudio(myMic);

      ns.publish(mySoundFile, "record");

      recordingState = "recording";

      setPBstatus('recording');

      myTimer.start();

      }

       

      private function record_stop(event:Event):void{

      switch (recordingState) {

      case "recording" :

      ns.close();

      myTimer.reset();

      timeText.text = createCountdown(0);

      break;

      setStatus("stopped");

      case "playing" :

      //Alert.show("Stopping now");

      ns.play(false);

      ns.close();

      playProgress.visible = false;

      micLevel.visible = true;

      ns = new NetStream(nc);

      ns.attachAudio(myMic);

      break;

      }

      recordingState = "idle";

      setPBstatus('ready');

      initMic("0xcccccc");

      }

      private function record_play(event:Event):void{

      switch(recordingState){

      case "idle" :

      play_btn.styleName = "pauseStyle";

      play_btn.label = "pause";

      playSound();

      break;

       

      case "playing" :

      recordingState = "paused";

      play_btn.styleName = "playStyle";

      play_btn.label = "play";

      ns.togglePause();

      break;

       

      case "paused" :

      play_btn.label = "pause";

      play_btn.styleName = "pauseStyle";

      recordingState = "playing";

      ns.togglePause();

      break;

      }

       

      }

      private function playSound():void{

      playProgress.visible = true;

      micLevel.visible = false;

      recordingState = "playing";

      ns.close();

      setStatus("buffering");

      var myClient:Object = new Object();

      ns = new NetStream(nc);

      ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

      ns.bufferTime = 5;

      ns.client = myClient;

      myClient.onMetaData = function(myMeta:Object):void{

      myDuration = myMeta["duration"];

       

      playbackTimer = new Timer(1000, myDuration);

      playbackTimer.addEventListener("timer", playbackTimerHandler);

      timeText.text = createTime(0);

      playbackTimer.start();

       

      }

      myClient.onPlayStatus = function(myPBstatus:Object):void{

      if(myPBstatus["code"] == "NetStream.Play.Complete"){

      setStatus("Playback complete");

      recordingState = "idle";

      play_btn.styleName = "playStyle";

      ns = new NetStream(nc);

      ns.attachAudio(myMic);

      playProgress.value = 0;

      playProgress.visible = false;

      micLevel.visible = true;

      }

      recordingState = "idle";

      }

      ns.play(mySoundFile);

      }

      public function playbackTimerHandler(event:TimerEvent):void {

      timeText.text = createTime(ns.time);

      playProgress.value = (ns.time / myDuration) * 10;

      //timeText.text = ns.time.toString();

      }

      private function scanPosition():void {

      ns.play(false);

      //divide by 10 because the Slider values go from 0 - 10 and we want a value

      //between 0 - 1.0

      ns.play(mySoundFile);

      ns.seek (playProgress.value/10 * myDuration);

      }

      private function showMicLevel(event:Event):void{

      switch (recordingState){

      case "recording" :

      micLevel.setProgress(myMic.activityLevel, 100);

      break;

      case "idle" :

      micLevel.setProgress(myMic.activityLevel, 100);

      break;

      case "playing" :

      micLevel.setProgress(ns.time, myDuration);

      timeText.text = createCounter(ns.time);

      break;

      }

      }

      private function setPBstatus(msg:String):void{

      micLevel.label = msg;

      }

       

      private function initMic(myColor:String):void{

      micLevel.setStyle("barColor", myColor);

      }

      private function createCountdown(time:int):String{

      time = timeLimit - time;

      var myBuffer:String = "0";

      var currentMins:int = Math.floor(time/60);

      var currentSecs:int = (time - (currentMins*60));

      var mySecs:String = ((currentMins < 10) ? myBuffer + currentMins.toString(): currentMins.toString())

      + ":"

      + ((currentSecs < 10) ? myBuffer + currentSecs.toString(): currentSecs.toString());

       

      return mySecs;

      }

      private function createCounter(time:int):String{

      var myBuffer:String = "0";

      var currentMins:int = Math.floor(time/60);

      var currentSecs:int = (time - (currentMins*60));

      var mySecs:String = ((currentMins < 10) ? myBuffer + currentMins.toString(): currentMins.toString())

      + ":"

      + ((currentSecs < 10) ? myBuffer + currentSecs.toString(): currentSecs.toString());

       

      return mySecs;

      }

      private function createTime(time:int):String{

      //time = timeLimit - time;

      var myBuffer:String = "0";

      var currentMins:int = Math.floor(time/60);

      var currentSecs:int = (time - (currentMins*60));

      var mySecs:String = ((currentMins < 10) ? myBuffer + currentMins.toString(): currentMins.toString())

      + ":"

      + ((currentSecs < 10) ? myBuffer + currentSecs.toString(): currentSecs.toString());

       

      return mySecs;

      }

      public function timerHandler(event:TimerEvent):void {

      if(myTimer.currentCount < timeLimit){

      timeText.text = createCountdown(myTimer.currentCount);

      } else {

      myTimer.reset();

      record_stop(null);

      }

      }

      private function setStatus(msg:String):void{

      statusText.text = msg;

      }

      ]]>

      </mx:Script>

      <mx:Text id="statusText" text="" width="320" height="25" y="124" x="3" textAlign="center"/>

      <mx:Text id="timeText" text="00:00" width="323" height="23" y="109" x="0" textAlign="center"/>

      <mx:Text id="titleText" text="Audio Recorder" width="317" height="50" y="0" x="3" textAlign="center" color="#0000FF" fontSize="24" fontWeight="bold"/>

      <mx:ProgressBar x="0" y="36" mode="manual" id="micLevel" label="" labelPlacement="bottom" width="320" themeColor="#ff0000" fontSize="10" fontWeight="normal"/>

      <mx:ControlBar x="0" y="63" width="320" height="45" horizontalAlign="center">

       

      <mx:Button

      label="record"

      id="record_btn"

      width="75"

      borderColor="#F71F09"

      color="#009900"

      icon="@Embed(source='icons/record_grey.png')"

      />

      <mx:Button

      label="stop"

      id="stop_btn"

      width="75"

      height="35"

      color="#009900"

      icon="@Embed(source='icons/stop_grey.png')"

      />

      <mx:Button

      label="play"

      id="play_btn"

      width="75"

      borderColor="#1005F6"

      color="#009900"

      styleName="playStyle"

      />

      </mx:ControlBar>

      <mx:HSlider change="scanPosition()" id="playProgress" showTrackHighlight="true" width="320" x="0" y="34" visible="false"/>

      <mx:Text id="versionText" text="v2009.1.22.03" width="320" height="15" y="134" x="3" textAlign="right" fontSize="8"/>

       

       

      </mx:Application>