×
Namespaces

Variants
Actions
Revision as of 23:41, 6 March 2008 by jaaura (Talk | contribs)

Archived:Communicating with Flash Lite from JavaScript in a widget (WRT)

From Nokia Developer Wiki
Jump to: navigation, search

Requirements:

Knowledge of Flash Lite

Knowledge of Javacript


Contents

Flash Lite

Steps:

1) Create a Flash File

On Frame 1

(actionscript layer)

Define these variables under action script window.

  var total_ram;
  var free_ram;


On Frame 5

Create New layer that holds the textboxes on the screen

(objects layer)

Create 2 textboxes on the screen in a separate layer and assign these variables in ‘var’ for the textboxes.

  Total_ram_txt   
  Free_ram_txt

Now Create a movie clip to indicate the memory level on the screen.

MemInfo.JPG

Note: when converting to symbol set the registration to as shown in the picture.(bottom)

MemInfo2.JPG


On Frame 5

(actionscript Layer)


   //Set the Interval of updating text boxes on the screen.
   var intervalId = setInterval( updateScreen, 1000 ); //1 Second
   function updateScreen()
   {
      if(total_ram != undefined && free_ram != undefined )
      {
          total_ram_txt = total_ram + " M";  //set Total ram in textfield
          free_ram_txt  = free_ram + " M";   //set Free ram in textfield
          _root.mem_lvl_mc._height = (free_ram*50/total_ram); //Update Level on the screen
      }
   }

SetInterval()function is a built-in action script function that preodically calls a piece of code or function after the set interval.

We are finished with the flash part.

Publish the swf file and name it as memInfo.swf.



Main HTML

Now create an html file with the following content.

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     
   http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
      <title>Memory Info</title>
      <script language="javascript">AC_FL_RunContent = 0;</script>
      <script src="AC_RunActiveContent.js" language="javascript"></script>
  </head>  
  <BODY BGCOLOR="#FFFFFF" onload = "load();"> 
      <embed id="systeminfo" type="application/x-systeminfo-widget" hidden="yes"></embed>
      <script language="javascript">
	    if (AC_FL_RunContent == 0) {
		 alert("This page requires AC_RunActiveContent.js.");
	    } else {
		 AC_FL_RunContent(
			'codebase', '[http]://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
			'width', '240',
			'height', '320',
			'src', 'network_signal240x320',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'window',
			'devicefont', 'false',
			'id', 'memInfo',
			'bgcolor', '#ffffff',
			'name', 'memInfo',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', 'memInfo',
			'salign', ''
			); //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=7,0,0,0',
	     ID=memInfo align="middle">
	     <param name="allowScriptAccess" value="sameDomain" />
	     <param name="allowFullScreen" value="false" />
	     <param name="movie" value="memInfo.swf" />
	     <param name="quality" value="high" />
	     <param name="bgcolor" value="#ffffff" />
	   <embed 
	      src="memInfo.swf" 
	      quality="high" bgcolor="#ffffff" 
	      width="240" 
	      height="320" 
	      name="memInfo" 
	      align="middle"
	      allowScriptAccess="sameDomain" 
	      allowFullScreen="false" 
	      type="application/x-shockwave-flash"    
              pluginspage="http://www.macromedia.com/go/getflashplayer" />
         </OBJECT>
         </noscript>
         </div>
         </BODY>
         </HTML>


Note:

<embed id="systeminfo" type="application/x-systeminfo-widget" hidden="yes"></embed>

Is used to allow widget to gain access to system attributes such as battery level,signal level, available memory and etc.

It needs to be defined under <body> tag.

The value inside ‘name’ property of flash embedded object will be used by javascript to communicate with flash. (e.g “memInfo”)



Javascript

In the Javscript file


  //USER DEFINED VARIABLES
  var sysinfo = null;
  var drives = 0;
  var totalRamSize;
  var freeRamSize;
  var space;
  var loop = 0;
  //---USER FUNCTIONS
  function load()
  {
     sysinfo = document.embeds[0]; //Must for accessing system attributes 	
     Runtimer();	
  }
  function Runtimer()
  {
  //Calls itself every 2 second and calls AC_communicateWithFlash() function
  t=setTimeout("Runtimer()",2000); //2 Second
  if(loop != 0)
  { AC_communicateWithFlash();}
    loop = 1;
  }
 function checkRamSpace()
 {
   // get total RAM size in bytes 
   totalRamSize = sysinfo.totalram; 
   space = Number(Number(totalRamSize) /  Number(1048576));  //MB
   totalRamSize = space.toFixed(2);
   // get free RAM size in bytes 
   freeRamSize = sysinfo.freeram; 
   space = Number(Number(freeRamSize) / Number(1048576));  //MB
   freeRamSize = space.toFixed(2);    //Display 2 Decimal Place
  }
  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);
    }
  }
  function AC_communicateWithFlash()
  {
  //this function checks for Current Ram Space and Drives Space
  //and sets the values inside flash.
   checkRamSpace();
  //memInfo defined in HTML file used for referencing flash Object
  var flashMovie=getFlashMovieObject("memInfo");
  //following steps assigns the JAVASCRIPT variables values to flash variables placed on  
    the root timeline.
  flashMovie.SetVariable("/:total_ram",totalRamSize); 
  flashMovie.SetVariable("/:free_ram",freeRamSize );
  }

Author

--raheal_akh 15:42, 5 December 2007 (EET) RAHEAL AKHTAR

131 page views in the last 30 days.