Why can't I get Javascript to talk to ActionScript
I have a very simple flash program that plays music. It consists of a play pause button and a timer that shows the current song position. I am trying to pause or play a song using a regular shape button.
<div class="musicplayer_playpause">
<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','65','height','68','src','player','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','wmode','transparent','id','flashobject','movie','player','flashvars','id=<?=$cur_songid;?>&type=<?=$_GET["type"];?>&csid=<?=$cur_songid;?>&l=<?=$Arrcntt+1;?>"' ); //end AC code
</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" id="flashobject" width="65" height="68">
<param name="movie" value="player.swf" allowscriptaccess="always"/>
<param name="quality" value="high" />
<param name="wmode" value="transparent" />
<param name="id" value="flashobject" />
<param name="swliveconnect" value="true" />
<embed src="player.swf" name="flashobject" width="65" height="68" quality="high" allowscriptaccess="always" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="transparent" swliveconnect="true" ></embed>
</object>
</noscript></div>
Here's the Javascript function I entered in the section:
<script type="text/javascript">
function Pause() {
var flash = document.getElementById('flashobject');
flash.PlayPause;
}
</script>
And finally, here's the button I'm using:
<form>
<input type="button" value="Play" name="Play" onClick="Pause();">
</form>
When I click the button the Firefox error console says "Flash is null" What am I doing wrong?
a source to share
I do not consider this to be a direct solution to your problem, but I know from experience that using
var flash = document.getElementById('flashobject');
flash.PlayPause;
will not show the same behavior in all popular browsers. you can try to use the following function to return an object:
function thisMovie(movieName) {
if(navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
};
thisMovie('flashobject').PlayPause();
If you are using jQuery follow these steps:
$("#flashobject")[0].PlayPause();
edit: I found a more updated version of thisMovie function. I would still recommend the jQuery way though :)
function getFlashMovieObject(movieName){
if(document.embeds[movieName])
return document.embeds[movieName];
if(window.document[movieName])
return window.document[movieName];
if(window[movieName])
return window[movieName];
if(document[movieName])
return document[movieName];
return null;
}
a source to share
The script tag containing Pause is incorrect. Add a trailing quote to the type attribute.
In addition, AC_FL_RunContent does not define an identifier for a movie. Add 4 more parameters: 'id', 'flashobject', 'name', 'flashobject'.
AC_FL_RunContent is described in this Adobe whitepaper http://kb2.adobe.com/cps/127/tn_12701.html , which says the following:
- id (attribute only for object, object)
Movie identifier. Identifies the Flash video in the host environment (a web browser, for example) so that it can be referenced using a scripted language.- name (embed only) Movie name. Defines a Flash movie for the host environment (web browser, usually), so it can be using a scripting language such as JavaScript or VBScript.
a source to share
Jasper has a proper JavaScript method, but you also need to add callbacks for your JS functions in ActionScript so the AS knows to listen to them.
AS code:
import flash.external.ExternalInterface;
ExternalInterface.addCallback("PlayPause", playPauseMovie);
...
function playPauseMovie():void {
//code goes here
}
a source to share