我应该如何判断闪存已经完成加载

时间:2012-11-21 10:00:41

标签: flash swfobject

在我们的网站上,我在html中嵌入了flash builder生成的flash,闪存大小超过2M。由于网络不良,可能需要30秒才能加载闪存。我怎么知道闪存已被浏览器完全加载?

2 个答案:

答案 0 :(得分:1)

您可以轮询SWF以获取其PercentLoaded值。

这是一种方法(从learnswfobject.com复制的代码):

function swfLoadEvent(fn){
    //Ensure fn is a valid function
    if(typeof fn !== "function"){ return false; }
    //This timeout ensures we don't try to access PercentLoaded too soon
    var initialTimeout = setTimeout(function (){
        //Ensure Flash Player's PercentLoaded method is available and returns a value
        if(typeof e.ref.PercentLoaded !== "undefined" && e.ref.PercentLoaded()){
            //Set up a timer to periodically check value of PercentLoaded
            var loadCheckInterval = setInterval(function (){
                //Once value == 100 (fully loaded) we can do whatever we want
                if(e.ref.PercentLoaded() === 100){
                    //Execute function
                    fn();
                    //Clear timer
                    clearInterval(loadCheckInterval);
                }
            }, 1500);
        }
    }, 200);
}

//This function is invoked by SWFObject once the <object> has been created
var callback = function (e){

    //Only execute if SWFObject embed was successful
    if(!e.success || !e.ref){ return false; }

    swfLoadEvent(function(){

        //Put your code here
        alert("The SWF has finished loading!");

    });

};

swfobject.embedSWF("movie.swf", "flashcontent", "550", "400", "9", false, false, false, false, callback);

答案 1 :(得分:0)

pipwerks答案工作正常。 你甚至可以获得更低的价值:而不是1500,只有100可以。

但我在Firefox上遇到了一些问题。 而不是超时,initialTimeout应该是一个timeInterval,因为在FF有时,在第一次调用时你将e.ref.PercentLoaded未定义,但下一次调用将没有问题。 当然你需要调用clearInterval(initialTimeout); if if是真的。

所以你会得到像:

    var initialTimeout = setInterval(function (){
     if(typeof e.ref.PercentLoaded !== "undefined" && e.ref.PercentLoaded()){
         clearInterval(initialTimeout);