0

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!

Giorgio Barchiesi
  • 6,109
  • 3
  • 32
  • 36
  • I would go with the ExternalInterface call, have you tried an alert to see if it works? -> ExternalInterface.call("alert","Call from Flash"); – Shaeldon Feb 24 '15 at 10:59
  • I think that your code is fine but if you are trying it in local may be you will get a security exception so try it on a web server. – akmozo Feb 24 '15 at 11:19
  • @Shaeldon SecurityError: Error #2060: Security sandbox violation: ExternalInterface caller file:///C:/...../myflash.swf cannot access file:///C:/...../page.html. Looks like akmozo is right, after all. – Giorgio Barchiesi Feb 24 '15 at 18:12
  • What strikes me is that I get the same security error after setting allowscriptaccess to "always"; adobe documentation says the call in this case should be always permitted – Giorgio Barchiesi Feb 24 '15 at 18:27

3 Answers3

1

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

Banovsky
  • 31
  • 7
  • SecurityError: Error #2051: Security sandbox violation: file:///C:/...../myflash.swf cannot evaluate scripting URLs within file:///C:/...../mypage.html (allowScriptAccess is sameDomain). Attempted URL was javascript:terminate(). – Giorgio Barchiesi Feb 24 '15 at 18:07
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.

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.

akmozo
  • 9,829
  • 3
  • 28
  • 44
0

It was actually a matter of security, due to the local environment, as suggested by @akmozo. I went to the global security settings of Flash Player:

http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html

and added the root folder of the tree contaning all HTML pages / SWF objects to the allowed locations. So now it also works locally.

Giorgio Barchiesi
  • 6,109
  • 3
  • 32
  • 36
  • I thought put an answer to explain all that ;) Yes, this is for Flash Player PPAPI (like Opera and Chrome), for Flash Player ActiveX (like IE) and NPAPI (like firefox), it's in the control panel > Flash Player > Advanced tab > Trusted Location Settings... or simply right click > Global Settings... – akmozo Feb 24 '15 at 19:14
  • @akmozo Yes, please, if you post tour answer I will choose and vote it – Giorgio Barchiesi Feb 25 '15 at 10:50