views:

800

answers:

4

Hi guys, my question today deals with Flash AS3 video buffering. (Streaming or Progressive) I want to be able to detect when the video is being buffered, so I can display some sort of animation letting the user know to wait just a little longer.

alt text

Currently my video will start up, hold on frame 1 for 3-4 secs then play. Kinda giving the impression that the video is paused or broken :(

Update

Thanks to iandisme I believe I'm faced in the right direction now. NetStatusEvent from livedocs. It seems to me that the key status to be working in is "NetStream.Buffer.Empty" so I added some code in there to see if this would trigger my animation or a trace statement. No luck yet, however when the Buffer is full it will trigger my code :/ Maybe my video is always somewhere between Buffer.Empty and Buffer.Full that's why it won't trigger any code when I test case for Buffer.Empty?

Current Code

public function netStatusHandler(event:NetStatusEvent):void 
  {
     // handles net status events
     switch (event.info.code) 

        {
            case "NetStream.Buffer.Empty":
                  trace("☼☼☼ Buffering!"); //<- never traces
                  addChild(bufferLoop);    //<- doesn't execute
            break;

            case "NetStream.Buffer.Full":
                  trace("☼☼☼ FULL!");      //<- trace works here
                  removeChild(bufferLoop); //<- so does any other code
            break;

            case "NetStream.Buffer.Flush":
                  trace("☼☼☼ FLUSH!");
                  //Not sure if this is important
            break
        }
    }
+2  A: 

Are you using a custom-rolled player? I know the FLVPlayback class has a buffering event built-in.

If you're not using FLVPlayback, the NetStream object fires a netStatusEvent that includes an info object every time it starts or stops buffering. You should be able to capture that event and play/hide your animation with that.

iandisme
Ah cool thanks for pointing me in the right direction :) I'm trying to use "NetStream.Buffer.Empty" now, but I never get a trace out of it :(
Leon
A: 

You can try checking NetStream's bufferTime and bufferLength every 100 milliseconds and take decision based on that. NetStream's bufferTime tells how long it should buffer before playing it and bufferLength tells how long it has already in the buffer.

function onTimerEvent(e:TimerEvent):void {
  var percent:Number = Math.round(ns.bufferLength/ns.bufferTime100 * 100);
  if (percent >= 95 && contains(bufferLoop)) {
    removeChild(bufferLoop);
  }
  if (percent < 25 && !contains(bufferLoop)) {
    addChild(bufferLoop);
  }
}

bhups
Did you mean (ns.bufferLength/ns.bufferTime * 100) ? I'm still not getting test traces to trace out with this, but still trying thx!
Leon
A: 

Bam I got it! Checking iandisme's answer since that help spawn this fix :)

Basically Buffer.Empty never got triggered, but Buffer.Full did. So basically I just added my buffering animation code right above the ns.play code:

addChild(bufferLoop); // add buffering animation
ns.play(flvUrl); videoStatus = "IsPlaying";

Then using NetStream.Buffer.Full to remove the animation :D

case "NetStream.Buffer.Full":
      removeChild(bufferLoop); // remove animation
break;
Leon
A: 

I came across this and thought I would share. Leon above mentions that the "NetStream.Buffer.Empty" doesn't trace anything. That is because you're loading your FLV from your local machine so the buffer is never empty. It works when you're actually streaming online.

All I did was set the clip I wanted as the "waiting buffer" visible when the movie started playing then used this code to make it go away and come back. Worked like a charm.

enter code here

var vidplaying:Boolean = false;

playmovie_btn.addEventListener(MouseEvent.CLICK, playmovie);

function playmovie(event:MouseEvent):void{

vidplaying = true;
wait_mc.visible = true;
    (yoru flv and netstream stuff) ect

}

ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

function netStatus(e:NetStatusEvent) {

    if(vidplaying == true && e.info.code == "NetStream.Buffer.Empty"){
        wait_mc.visible = true;
    }
    if(e.info.code == "NetStream.Buffer.Full"){
        wait_mc.visible = false;
    }

}// netStatus

David