PDA

View Full Version : Total Buffering with netstream


Sekter
01-14-2008, 10:19 PM
I was wondering how it would be possible to buffer content upto 100% before the user can actually view the content? Logically i would have thought it would have been something like the below code. And then have a statement which says: if (stream.bytesLoaded == stream.bytesTotal)
{
//Play the movie
}
else{ // continue buffering and do something else }
However it seems that when i call the bytesLoaded it is always at 0 :S. Any ideas anyone?

import flash.net.NetConnection;
import flash.net.NetStream;


var stream:NetStream;
var clientObj:Object = new Object();

var connection:NetConnection = new NetConnection();
connection.connect(null);
stream = new NetStream (connection);
stream.load("http://somewebsite/flv.flv");
my_video.attachNetStream(stream);
stream.client = clientObj;

duration_txt.text = stream.bufferLength + "";

clientObj.onMetaData = onMetadata;
function onMetadata (data:Object):void
{
var duration:Number = data.duration;
//duration_txt.text = String(duration);
}

hangalot
01-14-2008, 10:25 PM
not quite that code as you never set the buffer :)

i would suggest a different approach. set your movie to a low buffer then on each buffer full event you increase it.

panel
01-15-2008, 06:24 AM
@hangalot aproach seems ok. I would also check if metadata was loaded before increasing buffer, becouse if it was you can do sth like this

bufferTime = data.duration;


or
also I would consider what happens if we set bufferTime to ex 20secunds when movie length is 10s (mayby it will set buffer time to max...)

Sekter
01-15-2008, 08:38 AM
Thx both for replies, both good valid points, i think il try panel's suggestion tho as i dont want to buffer the movie while its playing hangalot unless i misunderstood wat u meant lol -sry if i did. Out of curiousity though why is it that when i use netstream and get the bytesLoaded its always zero or = bytesTotal once its cached? :confused:

hangalot
01-15-2008, 03:32 PM
panel suggestion is a bad one in practise. yoyr video won't play properly

Sekter
01-15-2008, 08:29 PM
Yeah hangalot but you said to set your movie to a low buffer then on each buffer full event you increase it. If i understand what you are saying here it is that everytime the video buffers you increase the buffer time. But i dont want the video to buffer at all i just want to buffer 100% and then play the video without worrying about buffering. Like i said im sure i misunderstood you? Is that what you meant?

hangalot
01-15-2008, 08:33 PM
i understand what you are trying to do, good luck in ever getting it solid accross all streaming video providers.
i am giving you a sollution which has been borne out of a couple of years video experience as well as seeing several video players code from several companies. i would not say such things like

panel suggestion is a <b>bad one in practise</b>.

i would suggest a different approach.
if i did not mean it :)

Sekter
01-15-2008, 08:40 PM
lol il implement your advice then and compare (even tho i shud take your word for it from experience :p). You know the strange thing at the moment is that i tried to use an flvplayback rather than netstream to stream the video and then i used VideoEventProgress events to catch if the video is stil loading and it worked :S. so it was something like:

PlayerVideo.addEventListener(VideoProgressEvent.PR OGRESS,onProgress);
function onProgress(pe:ProgressEvent):void
{
var loaded:uint = Math.round((PlayerVideo.bytesLoaded/PlayerVideo.bytesTotal)*100);
bufferRate_txt.text = loaded + "%";
if(PlayerVideo.bytesLoaded == PlayerVideo.bytesTotal)
{ //Can play the video now
buffer_txt.text = "You can view the video now"
}
else //Video buffering
{
buffer_txt.text = "The video is buffering please wait until the buffer bar disappears."
}
}

In this case the video actually loaded up 100% before playing but i have a feeling that something is dodgy about using this method. What do you think?

hangalot
01-15-2008, 08:41 PM
bytesloaded and bytestotal is for progressive video, a whole different kettle of fish

Sekter
01-15-2008, 08:48 PM
well yeah i dont have any streaming software on the server its progressive. So in the progressive case does your advice still apply if i want to buffer upto 100%?

panel
01-16-2008, 08:35 AM
panel suggestion is a bad one in practise. yoyr video won't play properly

It was my guess VS you experience in this matter, so you propably right:rolleyes: .You make me wonder why not to set buffer time to duration taken from metadata? What problems have you encountered?

Sekter
01-16-2008, 08:02 PM
well an update on the progress of ma testing. I have used the bufferLength = duration of the file. Next i have used a timer and set a delay of the timer to bufferlength (same as duration)*1000 since its in ms.
so the code structure is similar to:

var delay:Number = PlayerVideo.bufferTime*1000;
var timer:Timer = new Timer(delay,1);
timer.addEventListener(TimerEvent.TIMER,function onTimer(event:TimerEvent):void
{
//hide all the controls and
buffer_txt.text = "The video is buffering";
});
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function onComplete(event:TimerEvent):void
{
//remove all text displaying buffering
//enable the play button and other controls
});
timer.start();

This piece of code works with only 1 small problem. I cant figure out a way to show progress any ideas anyone ???

hangalot
01-17-2008, 03:14 AM
the video starts playing when your buffer time is reached while in an ideal enviroment still buffering. if you wait for it to completely buffer its the same as doing a complete download over progressive for the best case scenario vs trusting the netstream to function with that kind of buffer in a streaming enviroment. netstream, netconnection rtmp, rtmpt several ports, these things have quirks, firewalls and just low level socket legacy - video is a little m1nd#u*k in its own sphere.
progressive opens a different kettle of fish

Sekter
01-17-2008, 08:24 AM
Yeah hangalot your right, setting the duration the same as the bufferTime isnt good becoz for ppl who have a slow connection this wont work (If the video was 2 minutes and someone with a dialup was accessing then it might take 4 mins over the connection to buffer which doesnt address ma problem). By the way im not using netstream at the moment i am testing out things using FLVplayback to buffer and transfer data. I have it acting as progressive so far so that it acts like its downloading to completion but i am running out of ideas to be honest nothing is seeming concrete to build ideas on, What do you recommend hangalot is what im trying to do possible at all lol ? :confused: