How can I start a flash video from javascript?

纵然是瞬间 提交于 2019-12-18 09:19:41

问题


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?


回答1:


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.




回答2:


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




回答3:


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/




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/668377/how-can-i-start-a-flash-video-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!