Skip to main content
September 13, 2010
Question

Recording and Playing back Streaming Audio and Video

  • September 13, 2010
  • 1 reply
  • 924 views

Hi Folks...

I posted this in another forum too. I have this sample code from the e-book: "Learning Flash Media Server 3". The purpose of the code is to create a FLV. The code is not running as it should. When I click on the 'record' button, the label is to change to 'recording', that dosen't seem to happen and also when I click on the 'Stop Record' button, I get the following error:

"TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MinRecord/stopRecord()"

I have my suspicion on a certain section of the code as the possible culprit, but I am not sure how to rectify it, I will post the entire code here so you guys can have a look.

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.net.ObjectEncoding;
      import flash.media.Camera;
      import flash.media.Microphone;
      import flash.media.Video;


      public class MinRecord extends Sprite
      {
       private var nc:NetConnection;
       private var ns:NetStream;
       private var rtmpNow:String;
       private var msg:Boolean;
       private var cam:Camera;
       private var mic:Microphone;
       private var vid1:Video;
       private var recordBtn:Button;
       private var stopBtn:Button;
       private var textInput:TextInput;

       //Constructor
       function MinRecord ()
       {
       //NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
       nc=new NetConnection();
       nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect);
       rtmpNow="rtmp://192.168.0.11/vid2/recordings";
       //rtmpNow="rtmp:/vid2";
       nc.connect (rtmpNow);
       addMedia ();
       addUI ();
       recordBtn.addEventListener (MouseEvent.CLICK,startRecord);
       stopBtn.addEventListener (MouseEvent.CLICK,stopRecord);
       }


     private function addMedia ():void
      {
       cam=Camera.getCamera();
       cam.setMode (240,180,24);
       cam.setQuality (0,90);
       mic=Microphone.getMicrophone();
       vid1=new Video(cam.width,cam.height);
       vid1.attachCamera (cam);
       addChild (vid1);
       vid1.x=100;
       vid1.y=50;
       }


       private function addUI ():void
       {
       recordBtn=new Button();
       recordBtn.label="Record";
       recordBtn.x=100;
       recordBtn.y=50+(cam.height) +5;
       recordBtn.width=70;
       addChild (recordBtn);
       stopBtn=new Button();
       stopBtn.label="Stop Record";
       stopBtn.x=recordBtn.x+85;
       stopBtn.y=recordBtn.y;
       stopBtn.width=75;
       addChild (stopBtn);
       textInput=new TextInput();
       textInput.x=recordBtn.x;
       textInput.y=recordBtn.y + 30;
       addChild (textInput);
       }


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


       private function startRecord (e:Event):void
       {
             if (ns)
             {
                   recordBtn.label="Recording";
                   ns.attachAudio (mic);
                   ns.attachCamera (cam);
                   ns.publish (textInput.text,"record");
             }
       }


       private function stopRecord (e:Event):void
       {
             recordBtn.label="Record";
             ns.close ();
       }
      }
}

My deduction is that the 'if' statement in the 'startRecord' function is not resolving to 'true' and hence the label is not changing to 'Recording'. Also I feel that the assingment to the variable 'ns' of type NetStream is not being done and hence in the function 'stopRecord' I get the above mentioned error message when I click on the stop button. How do I rectify these problems?

    This topic has been closed for replies.

    1 reply

    Adobe Employee
    September 13, 2010

    I tried your code which you have pasted here and it works fine without any modification except "rtmpNow" string which I set it to my server. Check if the application directory exists and it has write permissions.

    The error which you got comes when you try to stop the recording and it has not started only. So please check these things at your end and it should work definitely.

    Regards,

    Amit

    September 14, 2010

    Hi Amit,

    Thanks for the message, I had acutually realised late last evening that changing this:

    rtmpNow="rtmp://192.168.0.11/vid2/recordings"

    to this:

    rtmpNow="rtmp:/vid2"

    which is what you have also recomended solves one of the problems which is that of the startRecord function, as my server is running on my own local machine. However when I click on stop record, I am getting the following error:

    "Error #2044: Unhandled NetStatusEvent:. level=error, code=NetStream.Publish.BadName
    at MinRecord/checkConnect()"

    I am trying to figure this one out...if you have any clues, let me know. Thanks.

    September 14, 2010

    Hi

    I know the cause of the error now. I left the text box below the record button blank which resulted in an issue for naming the recorded flv I guess. Thanks.