mscaliza
mscaliza

Reputation: 51

Retrieve swf information with javascript

I need to get some information of a swf "e-learning" movie. I need to know in wich part of it the user has stoped watching and or if user has watched until it's end. I am not developing a LMS, so I need just simple features. Are there any way to retrieve this information from within flash with javascript? If so, do you know where can I find some documentation about it?

Thanks in advance.

Upvotes: 0

Views: 288

Answers (1)

Onur Yıldırım
Onur Yıldırım

Reputation: 33634

You can use the Adobe Flash's ExternalInterface API to communicate between Javascript and ActionScript. Browser Support: IE 5.0+, Firefox 1.0+, Safari 1.3+, Opera, Chrome (versions N/A but old enough).

Flash TO Javascript Communication;

In your AS3/Flash:

function sendToJS(obj:Object):void {
    if (ExternalInterface.available) {
        ExternalInterface.call("onFlashCall", obj);
    }
}

In your HTML/Javascript:

<head>
    <script type="text/javascript">
        function onFlashCall(obj) {
            console.log("Data received from Flash: ", obj);
        }
    </script>
</head>

Javascript TO Flash Communication;

In your AS3/Flash:

public function onJavascriptCall(obj:Object):String 
{ 
    console.log("Data received from Javascript: ", obj);
} 
ExternalInterface.addCallback("sendToFlash", onJavascriptCall);

In your HTML/Javascript:

<head>
    <script type="text/javascript">
        function sendToFlash(obj) {
            var flashElem = document.getElementById("flashObject");
            flashElem.sendToFlash(obj);
        }
    </script>
</head>

<body>
<!-- Embeded Flash SWF -->
<object id="flashObject"...> 
    ... 
    <embed name="flashObject".../> 
</object>
</body>

Important: In Flash, not to get a security error:
In the object tag for the SWF file in the containing HTML page, set the following parameter:

<param name="allowScriptAccess" value="always" />

In the SWF file, add the following ActionScript:

flash.system.Security.allowDomain(sourceDomain);

So, in your case:

Call sendToJS() method inside your player's corresponding event handlers:

protected function onStopClick(event:MouseEvent):void
{
    sendToJS("User has clicked the STOP button.");
}

protected function onPlaybackFinish(event:Event):void
{
    sendToJS("Playback is complete.");
}

Upvotes: 1

Related Questions