How to notify the HTML container that the SWF has finished, using swfobject

I am embedding a flash object (swf file) into an HTML page. The object is written in as3 and built using Flash Builder. Its purpose is to show some animation, then finish.

It is really important to me to be able to notify the container that the animation has finished, but I can't find anything that works. I am using swfobject version 2.2.

Tried both on Chrome 40 and IE 11.

HTML (sample):

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <script type="text/javascript" src="js/swfobject.js"></script>
    <script type="text/javascript">
        function flashFinished() {alert('finished!');}
    </script>
    <script type="text/javascript">
        var flashVars = {}
        var flashParams = {allowscriptaccess : 'sameDomain'}
        var flashAttributes = {id : 'myflash', name : 'myflash'}
        swfobject.embedSWF('myflash.swf', 'flashObject', '960', '720', '9.0.0', 'swf/expressInstall.swf',
                flashVars, flashParams, flashAttributes);
    </script>
</head>
<body>
    <div id="flashObject">
        <p>To view this page please make sure that an updated version of Adobe Flash Player is installed.</p>
    </div>
</body>

AS3 (sample):

package
{
    public class myflash extends Sprite
    {
        public function myflash()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            startPlay();
        }
        private function startPlay() : void {
            // do whatever, then make sure function finishPlay is called in the end
        }
        private function finishPlay(event:TimerEvent) : void {
            if (ExternalInterface.available)
                ExternalInterface.call('flashFinished');
        }
    }
}

My "flashFinished" function gets never called. Can anyone suggest what am I doing wrong? Thanks!


Solution 1:

To avoid that Flash player blocks your local swf to access to an external URL (internet, ...) or to communicate with javascript or some other actions that fire usually a security sandbox violation error, you have to add it (the swf) to the list of trusted locations like this :

For Flash Player PPAPI (like Opera and Chrome) :

  • Right click on your swf opened in the browser, then Global Settings... :

enter image description here

Which will open this page.

  • Then click on the Global Security Settings panel link at the right side, which will give you this page :

enter image description here

  • Then, as it's mentioned in the image, click on Edit locations... combo box, and Add location, which will give you this box :

enter image description here

You have just to type your swf location, or it's parent directory, or simply the whole partition like what I did in the image. Try to avoid "Browse ..." buttons, sometimes it doesn't work (at least for me). Confirm and close the page and refresh that of your swf.

For Flash Player ActiveX (like IE) and NPAPI (like Firefox) :

  • Right click on your swf opened in the browser, then Global Settings..., you can also go to your system control panel and open Flash Player :

enter image description here

  • Then go to Advanced tab and click Trusted Location Settings... button :

enter image description here

  • Then you have to add the swf location using Add.. button > Add File... or Add folder... button > select your file/dir or partition and press OK, Confirm and close all dialogs.

enter image description here

Then you have just to refresh your page.

Solution 2:

In case the job of flash code is done and you don't need it anymore, just terminate the javascript in as3:

var exitStr:String = "javascript:terminate()";
navigateToURL(new URLRequest(exitStr),"_self");

And then add a onUnload function in HTML which will be called at that point:

onUnload="flashFinished();"

note: I used this code with swfobject v1.4 not sure about v2.2