8

My product opens a web browser and points it at an HTML file containing a local Flash application. How do I detect programmatically whether this file loaded successfully and if not what exception was thrown? Is there a way to do this using Javascript?

Checking externally whether the file exists on disk is not enough because I've seen other failures occur (race conditions might be involved).

4 Answers 4

12

Answering my own question: https://sourceforge.net/forum/message.php?msg_id=5929756

  1. Define a Javascript function that should be invoked if Flash loaded.
  2. Invoke this method from the top of your Flash file.
  3. Use a timer to detect if the callback is never invoked.
  4. Prefer invoking Javascript functions from Flash rather than invoking Flash functions from Javascript. Either way you cannot invoke a function that has not been loaded yet. It is far easier to guarantee that the browser has finished loading your Javascript function before invoking it from Flash, than guaranteeing that Flash finished loading your Flash function before invoking it from Javascript.

Here is an example:

  • I am using swfobject to embed Flash.
  • I use FlashVars to tell Flash which Javascript function to invoke. This is useful if there are multiple Flash objects on the page.

Flash

var params:Object = LoaderInfo(this.root.loaderInfo).parameters;
if (ExternalInterface.available)
{
    var onLoaded:String = params["onLoaded"];
    if (onLoaded != null)
        ExternalInterface.call(onLoaded, true);
}

Javascript

var flashLoaded = false;
var flashTimer;

function onFlashLoaded()
{
    flashLoaded = true;
    clearTimeout(flashTimer);
}

function onFlashTimeout()
{
    if (!isFlashLoaded)
    {
        // Remove the Flash object in case it is partially loaded
        $("#videoFeed").empty();
        $("#videoFeed").append('<div id="flashObject"></div>');
        alert("Failed to load video player");
    }
    clearTimeout(flashTimer);
}

function connectToVideo()
{
    var flashvars = {};
    flashvars.onLoaded = "onFlashLoaded";

    var params = {};
    params.menu = false;

    var attributes = {};

    isFlashLoaded = false;
    flashTimer = setTimeout("onFlashTimeout()", 5000);

    swfobject.embedSWF("flash/VideoFeed.swf", "flashObject", "800", "600", "11", "expressInstall.swf", flashvars, params, attributes);
}
Sign up to request clarification or add additional context in comments.

1 Comment

brilliant! This would help a lot today :D
4

In cases where you cannot modify the swf and adding an ExternalInterface is not an option, you can still use Javascript to get the status of the swf. For example, you can call document.getElementById(swf_id).PercentLoaded() from Javascript, and wait for it to be 100.

That won't tell you what exception was thrown if the swf failed to load, but at least you will know for sure whether it loaded. Other useful calls are found here: http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html

2 Comments

can't you simply launch a SWF file that plays another SWF file? You control the wrapper, and it throws an exception if the nested SWF file throws one.
This link is returning a 404 now.
3

Actually, when the HTML page finishes loading, the Flash content may not be completely loaded yet. If the SWF isn't done loading, then it will appear to have failed.

The method I usually recommend is to have the SWF call a JavaScript function through ExternalInterface right away when the document class constructor is invoked. Basically, assume that the SWF has failed to load unless that JS function is called.

3 Comments

Fair enough, but I need a more concrete way to detect if a failure has occurred. A timeout is not reliable enough.
One problem with this method is that you can't have two instances of the flash on the same page. Or at least you don't know which-one has triggered the callback.
To tell which SWF triggered the callback, you can pass in a unique string or number as an identifier using FlashVars. When the SWF makes the call to JavaScript, it also passes the identifier in the arguments.
1

Acording to adobe ExternalInterface documentation: http://livedocs.adobe.com/flex/3/langref/flash/external/ExternalInterface.html some web browsers restrict calling a Javascript function through ExternalInterface in the document class constructor if a pop-blocker is enabled.

Are there any other solution to detect when the swf movie has been successfully loaded?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.