Question

Is it possible to start playing a file inside a flash player by making use of javascript code? If so, how would I do it?

Was it helpful?

Solution

Try using swfObject, you can make any actionscript function visible for javascript using ExternalInterface and declaring them into javascript. So you can trigger actionscript function with play() (or any other code you want) from your javascript code.

Here is an example:

Actionscript:

import flash.external.ExternalInterface;

ExternalInterface.addCallback( "methodName", this, method );
function method() {
   trace("called from javascript");
}

Javascript:

function callAS() {
   swf.methodName(); 
}

Where methodName is the identifier js uses to call the method from actionscript.

OTHER TIPS

Take a look at SWFObject. There a lot of examples on how to accomplish that.

Yes it is. You can reference the flash movie objects from js and control the flash component in a page. Unfortunately the way you do it is not portable across browsers. See this:

http://www.permadi.com/tutorial/flashjscommand/

If you HAVE to do it from Javascript consider flipping ON the autoplay parameter like so:

Assuming you've grabbed the parent of the object/embed ( vidParent ):

if( document.all) {
  // toggle the object code (IE)
  vidParent.innerHTML = vidParent.innerHTML.replace(/0\" name=\"autoplay/gi,'1\" name=\"autoplay');
} else {
  // toggle the embed code
  vidParent.innerHTML = vidParent.innerHTML.replace(/autoplay=0/gi,'autoplay=1'); 
}

This will reload the flash with autoplay = 1 ( this example works with the YouTube player ).

I had to do this to do some tracking on video plays.

An interesting method is suggested here: http://www.permadi.com/tutorial/flashjscommand/

Works for me!

The idea is to get embed object using

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

and to call its Play() method afterwards.

getFlashMovieObject('MyMovie').Play()

A couple of other methods are supported, see the link above.

You can call any custom function in Flash from JavaScript, which requires you coding both Javascript and Flash.

See here for some examples: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_15683.

Also, using SwfObject helps a long way when dealing with Flash from JavaScript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top