views:

1256

answers:

3

Hi, I have a script that displays a list of song names and when the user clicks a 'listen' button the filename is passed to a Flash player which loads and plays the mp3. This works fine for Safari and IE but not in Mozilla. Does anyone know of any issues around Mozilla and using Javascript to pass variables to flash and call functions in flash.

In my header file I have -

<script type="text/javascript">
var flash;
  window.onload = function() {
     if(navigator.appName.indexOf("Microsoft") != -1) {
       flash = window.flashObject;
     }else {
       flash = window.document.flashObject;
     }
  }

AND

function PassFlash($preview_mp3){
   if(navigator.appName.indexOf("Microsoft") != -1) {
      window.flashObject.SetVariable("fileToPlay", $preview_mp3);
      window.flashObject.updatePlayer(); 
   }
   else {
     window.document.flashObject.SetVariable("fileToPlay", $preview_mp3);
     window.document.flashObject.updatePlayer();
  }

Then I embed the swf like so ...

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" name="flashObject" width="191" height="29" align="middle" id="flashObject">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="preview.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />
  <embed src="preview.swf" quality="high" bgcolor="#ffffff" width="191" height="29" name="flashObject" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

The swf is successfully loaded in all browsers (main ones) but in Firefox does not appear to receive the variables or function calls that javascript passes.

Many thanks in advance for any hints or tales of your own experience with this.

Stephen

+2  A: 

When using javascript to communicate with Flash, I've always had the least difficulty using swfObject. It's just a simple javascript lib that will embed the swf and make it easy to communicate back and forth. It works in all major browsers as well.

Alex Jillard
+3  A: 

+1 swfObject

I think what swfObject allows you to do is to write the Flashvars into the embed code, with the same result as if you were to hardcode the flashvars in. I think trying to change the hardcoded parts in your manner would be very similar to trying to change the flashvars during runtime, after the swf has already loaded. Firefox may well be loading the swf once it hits the html, not giving the javascript a chance to change the code.

also, read up on ExternalInterface.addCallback, that might be cool if you are compiling the swfs yourself.

Assembler
swfobject and ExternalInterface are absolutely the way to go.
Herms
Thanks guys, I use swfobject to embed Flash movies in all other areas of the site but had problems with it in this instance, for some reason the code wouldnt work when i used swfobject. I will try again...
undefined
A: 

Use swfObject. Read up the documentation for it. I'm sure you'll find it a breeze :)

Sherman