Skip to main content
Lazlo_Hollyfeld
Inspiring
October 8, 2009
Question

Microphone Activity Level at -1 even though Microphone.getMicrophone() is called, why?

  • October 8, 2009
  • 1 reply
  • 1145 views

Hi There,

I'm setting up an application where I need to record users' voice every so often during the course of the users' interaction with the application.  Recording is fine, and the quality is fine, but now my issue is understandng how Flash handles the microphone when it gets attached to a stream for publishing.

Basically, the problem is that when I "publish" my stream, the voice gets cut off (clipped) at the beginning.  So, I initially thought this may have to do with buffer settings on the FMS server side, but it wasn't.  Then I investigated client side stuff.  For some reason, my internal mic .activityLevel property is -1 even though I have called Microphone.getMicrophone().  According to Flash's documentation, this shouldn't be the case:

activityLevel    property
activityLevel:Number  [read-only]
Language Version :     ActionScript 3.0
Player Version :     Flash Player 9
The amount of sound the microphone is detecting. Values range from 0 (no sound is detected) to 100 (very loud sound is detected). The value of this property can help you determine a good value to pass to the Microphone.setSilenceLevel() method.

If the microphone is available but is not yet being used because Microphone.getMicrophone() has not been called, this property is set to -1.

Okay, I understand that fine.  So, here is my code.  I am using a timer to investigate the microphone .activityLevel property every 50 milliseconds:

package
{
    import fl.controls.Button;
    import fl.controls.TextInput;
    import flash.display.Sprite;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.NetStatusEvent;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.media.Microphone;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class MinRecord extends Sprite
    {
          private var nc:NetConnection;
          private var ns:NetStream;
          private var rtmpNow:String;
          private var msg:Boolean;
          private var mic:Microphone;
          private var recordBtn:Button;
          private var stopBtn:Button;
          private var textInput:TextInput;
       private var micTimer:Timer = new Timer(50, 0);
       private var recTimer:Timer = new Timer(4000, 1);
       private var recID:String = "userVoc";
       private var recording:Boolean = true;

          //Constructor
          function MinRecord ()
          {
            nc=new NetConnection();
               nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect);
               rtmpNow="rtmp://www.fmsurl.tv/app";
               nc.connect(rtmpNow);
                 
            mic = Microphone.getMicrophone();
            micTimer.addEventListener(TimerEvent.TIMER, showMicData);
                 
            recTimer.addEventListener(TimerEvent.TIMER, recVoice);
            recTimer.start();
          }
           
       private function showMicData(e:Event):void
       {
            trace( mic.name + "Level: " + mic.activityLevel); 
       }
           
       private function recVoice(e:TimerEvent):void
       {
          ns.close();               
       }
           
          private function addMedia():void
          {
             mic.rate = 22;
          }

          private function checkConnect (e:NetStatusEvent):void
          {
               msg=(e.info.code=="NetConnection.Connect.Success");
               if (msg)
               {     
                     ns = new NetStream(nc);
                      addMedia();
               startRecord();
               }
          }

          private function startRecord():void
          {
            trace("Start recording process");

               if (ns)
               {
               micTimer.start();
                     ns.attachAudio(mic);
                     ns.publish(recID,"record");
               trace("Speak for file:" + recID + ".flv");
               }
          }

          private function stopRecord ():void
          {     
               ns.close ();
          }
    }
}

Here are the trace statements from my output window when I run this program.  My question is why the level is at -1 at the time of publising the stream?  I already called everything I needed to in order for the client machine to attach mic data to the published stream.  I'm at a loss here.  I want to be sure that the user's voice does not get clipped at the start.

Start recording process
Speak for voice file:userVoc.flv
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: -1
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 4
Built-in MicrophoneLevel: 6
Built-in MicrophoneLevel: 5
Built-in MicrophoneLevel: 6
Built-in MicrophoneLevel: 8
Built-in MicrophoneLevel: 9
Built-in MicrophoneLevel: 12
Built-in MicrophoneLevel: 12
Built-in MicrophoneLevel: 10
Built-in MicrophoneLevel: 9
Built-in MicrophoneLevel: 7
Built-in MicrophoneLevel: 7
Built-in MicrophoneLevel: 5
Built-in MicrophoneLevel: 2
Built-in MicrophoneLevel: 3
Built-in MicrophoneLevel: 1
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0
Built-in MicrophoneLevel: 0

    This topic has been closed for replies.

    1 reply

    Lazlo_Hollyfeld
    Inspiring
    October 8, 2009

    Okay, after more investigation, I'm still getting clipped audio recordings even when the silence level is traced as 0 instead of -1.  Has anybody ever had the problem of clipped audio recordings after attaching mic data to a publishing stream?  Any tips as to how I might fix this?