Advertisement
Guest User

microphone recorder

a guest
Mar 31st, 2010
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:custom="com.talknicer.*"
  3.                 layout="absolute" creationComplete="init()"  width="400" height="206"
  4.                 backgroundColor="#C7C2C2" xmlns:components="components.*"
  5.                 xmlns:s="library://ns.adobe.com/flex/spark"
  6.                 >
  7. <mx:Script>
  8.     <![CDATA[
  9.         import flash.events.*;
  10.         import flash.media.*;
  11.         import flash.net.*;
  12.         import flash.utils.*;
  13.        
  14.         import mx.events.SliderEvent;
  15.        
  16.  
  17.             public static const INACTIVE_STAT:int = 0;
  18.             public static const PLAYBACK_STAT:int = 1;
  19.             public static const RECORDING_STAT:int = 2;
  20.             private var FLASH_SERVER:String = "rtmp://dev.llc.msu.edu/oflaDemo/RecordYourself";
  21.            
  22.            
  23.             private var _nc:NetConnection = null;
  24.             private var _inStream:NetStream = null;
  25.             private var _outStream:NetStream = null;
  26.            
  27.             private var _microphone:Microphone = null;
  28.            
  29.             private var _bConnected:Boolean = false;
  30.             private var _iStatus:int = 0;
  31.             private var _bMicReady:Boolean = false;
  32.             private var _bNetworkReady:Boolean = false;
  33.            
  34.             private var _internalFilename:String = genRandomStr(15);;
  35.            
  36.            
  37.            
  38.             public function getStatus():int {
  39.                 return _iStatus;
  40.             }
  41.            
  42.             public function get getMic():Microphone {
  43.                 return _microphone;
  44.             }
  45.             public function get fileName():String {
  46.                 return _internalFilename+".flv";
  47.             }
  48.            
  49.             private function setupMicrophone():void{
  50.                
  51.                 _microphone = Microphone.getMicrophone();
  52.                
  53.                 if (_microphone) {
  54.                     //_microphone.setLoopBack(true);               
  55.                     _microphone.codec = SoundCodec.SPEEX;
  56.                     _microphone.encodeQuality = 10;
  57.                     _microphone.rate = 16;//44;renam
  58.                     _microphone.framesPerPacket = 2;
  59.                     _microphone.gain = 50;
  60.                     _microphone.setUseEchoSuppression(false);
  61.                    
  62.                     //_outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
  63.                     //_outStream.addEventListener(IOErrorEvent.IO_ERROR, catchAll);
  64.                    
  65.                    
  66.                     //_microphone.rate = 16;  // is this a choice?!?
  67.                     //_microphone.framesPerPacket = 2;
  68.                     //_microphone.gain = 50;
  69.                     //_microphone.setUseEchoSuppression(true);  // I am hoping you can try this with both true and false
  70.                    
  71.                     _bMicReady = true;
  72.                    
  73.                     //outStream.attachAudio(mic);
  74.                     //outStream.publish("ttestaudio","record");
  75.                 }
  76.             }
  77.            
  78.             public function openConnection():Boolean{
  79.                
  80.                 _nc = new NetConnection();
  81.                 _nc.client = this;//cl;        
  82.                 _nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
  83.                 _nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
  84.                 _nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, catchAll);
  85.                
  86.                 _nc.connect(FLASH_SERVER);
  87.                
  88.                 return true;
  89.             }
  90.            
  91.            
  92.             private function netStatus(event:NetStatusEvent):void {
  93.                
  94.                 var info:Object = event.info;
  95.                
  96.                 switch (info.code){
  97.                    
  98.                     case "NetConnection.Connect.Success":
  99.                         _bNetworkReady = true;
  100.                         var ns:NetStream = new NetStream(_nc);
  101.                         ns.attachAudio(_microphone);
  102.                         break;
  103.                    
  104.                     case "NetConnection.Connect.Closed":
  105.                         closeConnection();
  106.                         break;
  107.                    
  108.                 }
  109.                
  110.             }      
  111.            
  112.             //private function onAsyncError(event:AsyncErrorEvent):void {}     
  113.             //private function onSecurityError(event:SecurityErrorEvent):void {}
  114.             public function onBWDone():void{}
  115.            
  116.             public function onPlayStatus(info:Object):void{
  117.                 trace("onPlayStatus:" + info.code);
  118.                 switch (info.code){
  119.                     case "NetStream.Play.Complete":
  120.                         stopPlayback();
  121.                        
  122.                         break;
  123.                 }
  124.             }
  125.             public function onMetaData(info:Object):void{
  126.                 trace("getting meta data");
  127.             }
  128.            
  129.             private function catchAll(event:Object):void {
  130.                 if (event.type == "netStatus") {
  131.                     trace("catchAll:code:" + event.info.code);
  132.                 } else {
  133.                     trace("catchAll:event:" + event);
  134.                 }
  135.             }
  136.            
  137.             private function playNetStatusHandler(event:NetStatusEvent):void {
  138.                 trace("my:"+event.info.code);
  139.                 switch (event.info.code) {
  140.                     case "NetStream.Pause.Notify":
  141.                         onVideoStop(null);
  142.                         //dispatchEvent(new Event(Event.COMPLETE));
  143.                         break;
  144.                 }
  145.             }
  146.            
  147.             public function closeConnection():void{
  148.                 if (_nc) _nc.close();
  149.                 _bNetworkReady = false;
  150.             }
  151.            
  152.             public function startPlaybackClass( fn:String = "" ):Boolean{
  153.                
  154.                 if ( _bNetworkReady && _iStatus == INACTIVE_STAT ){
  155.                    
  156.                     _outStream = new NetStream (_nc);      
  157.                     _outStream.client = this;
  158.                     _outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
  159.                     _outStream.addEventListener(IOErrorEvent.IO_ERROR, catchAll);
  160.                     _outStream.addEventListener(NetStatusEvent.NET_STATUS, playNetStatusHandler);
  161.                    
  162.                     if (fn == "") fn = _internalFilename;
  163.                     _outStream.play("flv:" + fn);
  164.                    
  165.                     _iStatus = PLAYBACK_STAT;
  166.                    
  167.                 }
  168.                
  169.                 return false;
  170.                
  171.             }
  172.            
  173.             public function genRandomStr( len:int ):String {
  174.                
  175.                 var tStr:String = new String();
  176.                 var lookup:String = "abcdefghijklmnopqrstuvwxyz1234567890_";
  177.                 for (var i:int = 0;i < len;i++){
  178.                     tStr += lookup.charAt(Math.floor(Math.random()*lookup.length));
  179.                 }
  180.                
  181.                 return tStr;
  182.             }
  183.            
  184.             public function startRecordClass():Boolean{
  185.                
  186.                 if (_bMicReady && _bNetworkReady && _iStatus == INACTIVE_STAT ){
  187.                     _inStream = new NetStream(_nc);
  188.                     //_outStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, catchAll);
  189.                     //_outStream.addEventListener(IOErrorEvent.IO_ERROR, catchAll);
  190.                     _inStream.addEventListener(NetStatusEvent.NET_STATUS, catchAll);
  191.                    
  192.                    
  193.                     _inStream.attachAudio(_microphone);
  194.                    
  195.                     //_internalFilename = genRandomStr(15);
  196.                    
  197.                     _inStream.publish(_internalFilename,"record");
  198.                     _iStatus = RECORDING_STAT;
  199.                     return true;
  200.                 }
  201.                
  202.                 return false;
  203.             }
  204.            
  205.             public function stopPlayback():void{
  206.                
  207.                 if (_iStatus == PLAYBACK_STAT ){
  208.                    
  209.                     _outStream.pause();
  210.                     _outStream.play(false);
  211.                     _iStatus = INACTIVE_STAT;
  212.                    
  213.                 }
  214.             }
  215.            
  216.             public function stopRecord():void{
  217.                
  218.                 if (_inStream) _inStream.close();
  219.                 _inStream = null;
  220.                 _iStatus = INACTIVE_STAT;
  221.                
  222.             }
  223.            
  224.            
  225.        
  226.         //---------------clases end
  227.        
  228.        
  229.         public static const RECORD_LABEL:String = "Record";
  230.         public static const STOP_LABEL:String = "Stop";
  231.         public static const PLAY_LABEL:String = "Play";
  232.        
  233.         public static const RECORD_STYLE:String = "recordStyle";
  234.         public static const STOP_STYLE:String = "stopStyle";
  235.         public static const PLAY_STYLE:String = "playStyle";
  236.        
  237.         private var recSprite:Sprite = new Sprite();
  238.         private var stopRecSprite:Sprite = new Sprite();
  239.         private var stopSprite:Sprite = new Sprite();
  240.         private var playSprite:Sprite = new Sprite();
  241.         private var submitSprite:Sprite = new Sprite();
  242.        
  243.         private function updateResultDisplay( str:String ):void {
  244.            
  245.         }
  246.        
  247.         private function init():void{
  248.             // Current release of FMS only understands AMF0 so tell Flex to
  249.             NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
  250.             SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
  251.             //NetStream.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
  252.            
  253.             openConnection();
  254.             setupMicrophone();
  255.             addEventListener(Event.ENTER_FRAME, showMicLevel);
  256.             //_recorder.addEventListener(Event.COMPLETE, onVideoStop);
  257.             var squareSize:uint = 10;
  258.            
  259.             //submitSprite.graphics.beginFill(0x00FF00);
  260.             submitSprite.graphics.lineStyle(2,0x00FF00,1);
  261.             submitSprite.graphics.moveTo(0,0);
  262.             submitSprite.graphics.lineTo(squareSize/2,squareSize);
  263.             submitSprite.graphics.lineTo(squareSize-2,0);
  264.             submitSprite.x = 5;
  265.             submitSprite.y = 5;
  266.             //submitSprite.graphics.endFill();
  267.            
  268.             playSprite.graphics.beginFill(0x000000);
  269.             playSprite.graphics.moveTo(0,0);
  270.             playSprite.graphics.lineTo(squareSize/2,squareSize/2);
  271.             playSprite.graphics.lineTo(0,squareSize);
  272.             playSprite.graphics.lineTo(0,0);
  273.             playSprite.graphics.endFill();
  274.             playSprite.x=8;
  275.             playSprite.y=5;
  276.            
  277.                        
  278.             stopRecSprite.graphics.beginFill(0x000000);
  279.             stopRecSprite.graphics.drawRect(0, 0, squareSize, squareSize);
  280.             stopRecSprite.graphics.endFill();
  281.             stopRecSprite.x=5;
  282.             stopRecSprite.y=6;
  283.            
  284.            
  285.             stopSprite.graphics.beginFill(0x000000);
  286.             stopSprite.graphics.drawRect(0, 0, squareSize, squareSize);
  287.             stopSprite.graphics.endFill();
  288.             stopSprite.x=5;
  289.             stopSprite.y=6;
  290.        
  291.             recSprite.graphics.beginFill(0xff0000, 1);
  292.            
  293.             recSprite.graphics.drawCircle(0, 0, 4);
  294.             recSprite.x=10;
  295.             recSprite.y=10;
  296.  
  297.             //recordButton.addChild(recSprite);
  298.             recCanvas.rawChildren.addChild(recSprite);
  299.             recCanvas.rawChildren.addChild(stopRecSprite);
  300.             playCanvas.rawChildren.addChild(playSprite);
  301.             playCanvas.rawChildren.addChild(stopSprite);
  302.             submitCanvas.rawChildren.addChild(submitSprite);
  303.             stopRecSprite.visible = false;
  304.             stopSprite.visible = false
  305.             //recordButton.setStyle("icon",recIcon);
  306.             //playButton.setStyle("icon", playIcon);
  307.             //sumbmitButton.setStyle("icon", checkmarkIcon);
  308.         }
  309.         private function onSubmitClick():void
  310.         {
  311.             navigateToURL(new URLRequest("http://talknicer.com/vadim.cgi?filename="+fileName),"_self");
  312.         }
  313.         private function onVideoStop(evt:Event):void
  314.         {
  315.             //playButton.styleName = PLAY_STYLE;
  316.             playButton.label = PLAY_LABEL;
  317.             playSprite.visible = true;
  318.             stopSprite.visible = false;
  319.             //playButton.setStyle("icon", playIcon);
  320.         }
  321.         private function micSlider_changeHandler(event:SliderEvent):void
  322.         {
  323.             if (getMic)
  324.                 _microphone.gain = 100-event.value;
  325.         }
  326.  
  327.         private function showMicLevel(event:Event):void
  328.         {
  329.             if (getMic)
  330.             {
  331.                 micLeval.setProgress(100-getMic.activityLevel, 100);
  332.             }
  333.         }
  334.  
  335.         private function startPlayback():void {
  336.            
  337.             switch ( getStatus() ){
  338.                
  339.                 case PLAYBACK_STAT:
  340.                     stopPlayback();
  341.                     //playButton.styleName = PLAY_STYLE;
  342.                     playButton.label = PLAY_LABEL;
  343.                     playSprite.visible = true;
  344.                     stopSprite.visible = false;
  345.                     //playButton.setStyle("icon",playIcon);
  346.                     break;
  347.                 case RECORDING_STAT:
  348.                     stopRecord();
  349.                     break;
  350.                 case INACTIVE_STAT:
  351.                     startPlaybackClass();
  352.                     //playButton.styleName = STOP_STYLE;
  353.                     playButton.label = STOP_LABEL;
  354.                     playSprite.visible = false;
  355.                     stopSprite.visible = true;
  356.                     //playButton.setStyle("icon",stopIcon);
  357.                     break;
  358.                
  359.             }
  360.            
  361.         }
  362.        
  363.         private function startRecord():void {
  364.            
  365.             switch ( getStatus() ){
  366.                
  367.                 case RECORDING_STAT:
  368.                     stopRecord();
  369.                     recordButton.label = RECORD_LABEL; 
  370.                     //recordButton.setStyle("icon",recIcon);
  371.                     stopRecSprite.visible = false;
  372.                     recSprite.visible = true;
  373.                     //recordButton.styleName = RECORD_STYLE;
  374.                     playButton.enabled = true;
  375.                     sumbmitButton.enabled = true;
  376.                    
  377.                     return;
  378.                    
  379.                 case PLAYBACK_STAT:
  380.                     stopPlayback();
  381.                     return;
  382.                    
  383.                 case INACTIVE_STAT:
  384.                     startRecordClass();
  385.                     recordButton.label = STOP_LABEL;
  386.                     recSprite.visible = false;
  387.                     stopRecSprite.visible = true;
  388.                     //recordButton.setStyle("icon", stopIcon);
  389.                     //recordButton.styleName = STOP_STYLE;
  390.                    
  391.                     return;
  392.             }
  393.            
  394.         }
  395.        
  396.  
  397.     ]]>
  398. </mx:Script>
  399.     <mx:Button enabled="false" left="0" right="0" bottom="0" top="0"/>
  400.     <mx:Canvas left="0" right="0" bottom="0" mouseChildren="false" top="0" />
  401.     <mx:Text id="titleText" text="Audio Recorder" width="317" height="50" textAlign="center" color="#0000FF" fontSize="24" fontWeight="bold" horizontalCenter="0" top="20"/>
  402.     <mx:VSlider height="60" x="27" y="90" maximum="100" minimum="0" value="50" id="micSlider" change="micSlider_changeHandler(event)" allowTrackClick="true" liveDragging="true" enabled="true" width="28"/>
  403.     <mx:Canvas x="78" y="38" width="88" height="94">
  404.    
  405.         <mx:ProgressBar mode="manual" label=" " height="9" id="micLeval" rotation="90" width="50" x="0" y="56" paddingLeft="0"/>
  406.     </mx:Canvas>
  407.     <mx:Form x="71" y="88">
  408.        
  409.         <mx:HBox x="50" horizontalGap="0">
  410.         <mx:Canvas id="recCanvas">
  411.            
  412.        
  413.             <mx:Button id="recordButton" click="startRecord()" width="75"
  414.                        borderColor="#F71F09"
  415.                        color="#009900"
  416.                        label="{RECORD_LABEL}"
  417.                        
  418.                        />
  419.         </mx:Canvas>
  420.         <mx:Canvas id="playCanvas">
  421.            
  422.        
  423.         <mx:Button enabled="false" id="playButton" click="startPlayback()" width="75"
  424.                    borderColor="#F71F09"
  425.                    color="#009900"
  426.                    label="{PLAY_LABEL}" />
  427.         </mx:Canvas>
  428.             <mx:Canvas id="submitCanvas">
  429.                
  430.    
  431.            
  432.            
  433.         <mx:Button label="Submit" id="sumbmitButton" click="onSubmitClick()"
  434.                    enabled="false" fontWeight="normal"
  435.                    />
  436.             </mx:Canvas>
  437.            
  438.         </mx:HBox>
  439.    
  440.     </mx:Form>
  441.    
  442.  
  443. </mx:Application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement