开发者

How to detect when video is buffering?

开发者 https://www.devze.com 2022-12-13 07:07 出处:网络
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 use

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.

How to detect when video is buffering?

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("☼开发者_StackOverflow中文版☼☼ 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
        }
    }


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.


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);
  }
}


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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消