Double Buffering for smooth netstream FLV playback
This post is to highlight the double buffering technique (also known as dynamic buffering) that “should” make video streaming in flash alot smoother without having to prebuffer anything.
This technique works on progressive video download as well as streaming:
var startBufferLength:Number= 2; //keep this in the range 2-4+
var xpandedBufferLength:Number = 15; //arbitrarily highnc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.setBufferTime(3);
ns.autoPlay = false;
ns.onStatus = Delegate.create(this, handleVideoStatus);//(if this was AS3 the syntax will be:
//ns.addEventListener(NetStatusEvent.NET_STATUS, handleVideoStatus);
// if this was Actionscript 3, this following line would be:
// private function handleStatus(event:NetStatusEvent):void
function handleVideoStatus(infoObject:Object)
{
switch (infoObject.code) {
case "NetConnection.Connect.Success":
break;
case "NetStream.Buffer.Full":
ns.bufferTime = xpandedBufferLength;
break;
case "NetStream.Buffer.Empty":
ns.bufferTime = startBufferLength;
break;
case "NetStream.Play.Start":
// the video has started playing, do something here
break;
case "NetStream.Play.Stop" :
// the video has stopped playing (finished)
break;
}
}
what is the infoObject in your code?
What does it do?
How do you create one?
Great info – keep up the great work.
I am experimenting with this in AS2 on a progressive video but not seeing any effect from this over a live site. Perhaps I need a very slow net connection to really test this.
tarik mou alil ya ibni
YOU ROCK!!!!!!!!!! THIS CODE IS JUST WHAT I WAS LOOKING FOR!!!!!! WIth a few modifications and implementation with this code:
http://www.ultrashock.com/forums/actionscript/as3-flv-preloader-91343.html
I was able to create a really good FLV player in AS3. Thanks ALOT!!!
Very helpful.. thanks!
One nitpick – on line 2 of your code listing:
nc = new NetConnection(); looks like it’s part of the commented line.
Thx again for sharing this excellent approach!
Your example won’t work as expected out of the box when implementing in AS2. The lines “ns.bufferTime = xpandedBufferLength” and “ns.bufferTime = startBufferLength” won’t do anything since bufferTime is a read-only property in AS2. The correct way to change the buffer time is used earlier in your code, through the method setBufferTime(). Besides that, good job.