# Implementing buffer in video recording app

**URL:** <https://forum.kirupa.com/t/implementing-buffer-in-video-recording-app/321768>\
**Category:** flash\
**Created:** [April 30, 2011, 8:04pm UTC](https://forum.kirupa.com/t/implementing-buffer-in-video-recording-app/321768 "2011-04-30T20:04:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fllashk123](https://avatars.discourse-cdn.com/v4/letter/f/b782af/32.png) [@fllashk123](https://forum.kirupa.com/u/fllashk123)\
**Post date:** [April 30, 2011, 8:04pm UTC](https://forum.kirupa.com/t/implementing-buffer-in-video-recording-app/321768/1 "2011-04-30T20:04:54Z")

</div>

Hi,

I am developing an webcam recorder and would like to implement the buffer concept in order to avoid the dropping of video frames.

Can any one please help in implementing it.

Find below code which I have implemented so far.

```auto
public var camera:Camera;
public var mic:Microphone;
public var nc:NetConnection;
public var ns:NetStream;	
public var nsOutGoing:NetStream;
var bufferCheckTimer:Timer=new Timer(60);

```

Now after getting “NetConnection.Connect.Success”, following gets called:

```auto
nsOutGoing = new NetStream(nc);
camera=Camera.getCamera();

var nsOutGoingObject:Object = new Object();
nsOutGoing.client = nsOutGoingObject; 
nsOutGoing.bufferTime = 60; 
nsOutGoingObject.onMetaData = handleOnMetadata;	
nsOutGoingObject.onPlayStatus = handleOnPlayStatus; 
myWebcam.attachCamera(camera);
nsOutGoing.attachCamera(camera);

camera.addEventListener(StatusEvent.STATUS, handleCameraStatus);

```

//Now Setting up the microphone and adding it to nsOutGoing object.

Now, once user clicks on the record button, following code is called:

```auto
nsOutGoing.publish(fileName, "record");
// Start the timer to update the buffer 
camera.addEventListener("timer", handleBufferCheck); 
bufferCheckTimer.start();

```

Now, when user clicks on stop button, following code is called:

```auto
if (nsOutGoing != null) 
{ 
nsOutGoing.attachAudio(null); 
nsOutGoing.attachCamera(null); 
videoPlayer.attachCamera(null); 
}

```

```auto

// Here's the timer's handler that continually checks the BufferLength 

private function handleBufferCheck(e:TimerEvent):void 
{ 
if (nsOutGoing != null) 
{ 

trace("Video buffer length: " + nsOutGoing.bufferLength); 
if ( (nsOutGoing.bufferLength == 0) && (streamsHalted == true) ) 
{ 
nsOutGoing.close(); 

// Stop the timer 
bufferCheckTimer.stop(); 

var s:String = Recorder.server+Recorder.fileName+".flv";
videoPlayer.source = s;
videoPlayer.stop();
} 
} 
}

```

Above code somehow is not working . Its not even loading the swf i guess.  
Now, another problem is that, what do I implement in handleOnMetadata and handleOnPlayStatus. Also, is setting up the buffertime is sufficient? How and where do I keep track of the buffer timer?

I am new to Action Script and would appreciate if any one would help in above code in fixing it and implementing the buffer.

Thanks,  
Vaibhav
