Flash AS2.0 and JavaScript / jQuery (ExternalInterface) Communication

I am trying to use JS to send data to my Flash AS2.0 music player with ExternalInterface, except that there are no good manuals or manuals that I can find on ExternalInterface. I want to be able to change the current song in the player by clicking the JavaScript link, and to load the page / window without clicking, play the default song.

I don’t need a super-complicated answer for loading sounds in a flash, etc. I'm just having trouble JS sends something to Flash, and when I get it to work - I will need to add some if / else to flash to determine if the link was clicked or not?

thanks

edit the code at the moment:

AS 2.0

import flash.external.ExternalInterface; ExternalInterface.addCallback('loadSong', null, flashFunction); function flashFunction (val) { extra = val; } 

Javascript

 var flashObj = document.getElementById('VSPLAYER'); function loadSong(val) { return val } 

HTML

 <a href="javascript:loadSong('2')">Play song 2</a> <object id="VSPLAYER" type="application/x-shockwave-flash" data="vs_player.swf" width="280" height="90"> <param name="movie" value="vs_player.swf" /> <param name="allowscriptaccess" value="always" /> </object> 
+4
source share
2 answers

In Flash, preferably in the first frame, put this:

 ExternalInterface.addCallback('callFlashFunction', null, yourFlashFunction); function yourFlashFunction (arg1, arg2) { ... } 

In your Javascript, you can call through:

 var flashObj = document.getElementById ('FlashObjID'); flashObj.callFlashFunction (arg1, arg2); 

If your SWF is in a different domain, remember to allow script access through:

 <object id="FlashObjID"> ... <param name="allowscriptaccess" value="always" /> ... </object> 

Update (based on your HTML):

 <a href="#" onclick="return jsLoadSong('2')">Play song 2</a> <object id="VSPLAYER" type="application/x-shockwave-flash" data="vs_player.swf" width="280" height="90"> <param name="movie" value="vs_player.swf" /> <param name="allowscriptaccess" value="always" /> </object> <script type="text/javascript"> var flashObj = document.getElementById ('VSPLAYER'); function jsLoadSong (val) { flashObj.loadSong (val); return false; // to prevent default link action } </script> 

I tried to clarify the names to show what is in Javascript and which is attached to Flash. Also note that you need to wait for the DOM to load before flashObj definition. here the <script> is after the <object> , so it will work fine.

+9
source

Thanks for the posts, but yes, this javascript call is also invalid for me. After SO grimacing a lot, it finally worked out:

 $(this)[0].loadSong(); 

(using jQuery where flashObj has already been selected)

Hope this helps someone.

Not sure what you need [0] if not using jQuery.

Read elsewhere that the actual HTML DOM object is selected in the first slot of the selector array. Sorry, my explanations are a little noobish, just hope this helps you get it to work.

+1
source

All Articles