How to detect when video is buffering?

↘锁芯ラ 提交于 2019-12-01 00:42:59

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!