Search code examples
apache-flexflashactionscript-3mxmlmxmlc

Microphone progress bar in action script


Any microphone action script code which indicates a progress bar when there is activity on microphone


Solution

  • here's some code you can use:

    it uses the graphics API to draw the indication of volume

    <?xml version="1.0" encoding="utf-8"?><mx:Application 
        xmlns:mx="http://www.adobe.com/2006/mxml" 
        layout="absolute" 
        width="450" 
        height="450"
        creationComplete="creationComplete_Handler()">
    
    <mx:Script>
        <![CDATA[
            import flash.media.Microphone;
            import flash.events.ActivityEvent;
            import flash.events.Event;
            import flash.events.StatusEvent;
    
            private var _mic:Microphone;
    
            private function creationComplete_Handler():void
            {
                _mic = Microphone.getMicrophone();
                _mic.setLoopBack(true);
    
                _mic.addEventListener(Event.ACTIVATE, mic_ActivateEventHandler);
                _mic.addEventListener(StatusEvent.STATUS, mic_StatusEventHandler);
                _mic.addEventListener(ActivityEvent.ACTIVITY, mic_ActivityEventHandler);
    
            }
    
            private function mic_ActivateEventHandler(e:Event):void
            {
                lblStatus.text = "mic active!";
            }
    
            private function mic_StatusEventHandler(e:StatusEvent):void
            {
                trace("ststus event: " + e.toString()); 
            }
    
            private function mic_ActivityEventHandler(e:ActivityEvent):void
            {
                trace("Activity Event");
                addEventListener(Event.ENTER_FRAME, mic_EnterFrame_EventHandler);
            }
    
            private function mic_EnterFrame_EventHandler(e:Event):void
            {
                lblMicLevel.text = "Mic Level :"+_mic.activityLevel.toString();
                micLevelCanvas.graphics.clear();
    
                micLevelCanvas.graphics.beginFill(000000, 1);
                micLevelCanvas.graphics.drawRect(0, 0, (_mic.activityLevel * 2), 20);
                micLevelCanvas.graphics.endFill();
            }
    
        ]]>
    </mx:Script>
    
    
    
    
    <mx:Label x="10" y="10" id="lblStatus"/>
    <mx:Canvas x="10" y="36" width="200" height="20" id="micLevelCanvas" />
    <mx:Label x="10" y="64" id="lblMicLevel"/></mx:Application>