4

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

I dont need a super complicated answer on loading sounds in flash, etc., I am just having a lot of difficulties getting JS to send anything to Flash, and when I get that to work - would I need to put some if / else into the flash to determine if the link has been clicked or not?

Thanks

edit heres the code as of now:

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>
abysslogic
  • 321
  • 3
  • 6
  • 15

2 Answers2

9

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

ExternalInterface.addCallback('callFlashFunction', null, yourFlashFunction);

function yourFlashFunction (arg1, arg2) { ... }

In your Javascript, you can call it via:

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

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

<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 which is in Javascript, and which is tied to the Flash. Also note that you'd need to wait till the DOM is loaded before calling defining flashObj. here, the <script> tag is after the <object>, so it will work fine.

K Prime
  • 5,809
  • 1
  • 25
  • 19
  • 1
    Make sure that Flash embedded in HTML have an id. This will make the ExternalInterface.addCallback function recognizable to javascript in IE. – Makram Saleh Jan 07 '10 at 08:54
  • the flashObj.callFlashFunction (arg1, arg2); doesnt seem to be valid, and stops my javascript from working at all. I have tried setting callFlashFunction as an actual function, and it still isnt passing the variable. – abysslogic Jan 07 '10 at 19:15
  • Check whether your flashObj is really the Flash object (or could be the `embed` obj in FF, depending on your markup), and check security permissions – K Prime Jan 08 '10 at 01:44
  • Im staying away from embed, as it is not standard compliant. Still no progress, I might try and post my code here if I can remember how and see if that changes anything.. – abysslogic Jan 08 '10 at 01:50
  • @abysslogic - you need to call `.loadSong ()` - maybe posting your HTML snippet would help – K Prime Jan 08 '10 at 02:28
  • The rest of the relevant HTML is up, where would I be calling .loadSong()? – abysslogic Jan 08 '10 at 02:50
  • @abysslogic - Your `jsLoadSong` should be outside `$(document).ready ( ... )` block, otherwise it's not visible to the entire document. Alternatively, you can attach it to the global object, immediately after defining eg. `window.jsLoadSong = jsLoadSong` – K Prime Jan 08 '10 at 06:42
  • ahh, I thought it was supposed to be inside as it has to called after the DOM loads. Anyways cant do anymore work on it tonight, have to setup on my new server and I can do anything cPanel, FTP, or webmail related (thanks entangle firewall..). But thanks for your help, you've definately pointed me in the right direction and I'll know tomorrow whether moving out of the document.ready will work or not – abysslogic Jan 08 '10 at 06:57
  • @abysslogic - If you wanted to keep it inside, you'll have to attach the event handler within the same scope as well, and not use `onclick`. Really up to you, hope it works out – K Prime Jan 08 '10 at 07:13
1

Thanks for the posts, but yes, that javascript call wasn't valid for me either. After SO much mucking around, finally got it to work with:

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

( using jQuery, where flashObj was already selected )

Hope this helps someone.

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

Read somewhere else that the actual HTML DOM Object is selected in the first slot of the array of the selector. Sorry my explanations are a bit noobish, just hope it helps you get it working.