Recently I had a project to build a Flv player with flash. The video file name and the title were to be passed to the flash file as parameters from the html file. Since I had not done passign parameters to flash file with AS 3.0 I search the net to see how its done. There were many tutorials and the solution seemed very simple and straight forward. So I left this part to the very end of the project. But when I tookup this part it didn’t work as expected. Finally I figured it out and thought that I will share it with all of you.
How to pass parameters to flash (swf) file
First we will begin with the passing the variable to the SWF file from the html file. This was where I went wrong. I tried the following ways that didn’t work for me.
<param value=”flashfile.swf?par1=hello&par2=world” …..
<param value=”flashfile.swf” flashvars=”par1=hello&par2=world” …..
<embed src=”flashfile.swf?par1=hello&par2=world” …….
<embed src=”flashfile.swf” flashvars=”par1=hello&par2=world” …..
finally what worked was including the parameter in the javascript as shown below.
AC_FL_RunContent(
‘codebase’, ‘http://……’,
‘width’, ’500′,
‘height’, ’400′,
‘src’, ‘flashfile’,
‘flashvars’, ‘par1=hello&par2=world’,
………..
)
or
AC_FL_RunContent(
‘codebase’, ‘http://……’,
‘width’, ’500′,
‘height’, ’400′,
‘src’, ‘flashfile?par1=hello&par2=world’,
………..
)
Publish your flash file and edit the html file generated to add the ‘flashvars’.
Accessing flash variables with actionscript 3.0
Now we need to access these variables from flash file with actionscript. The flash variables are included as properties of ‘root.loaderInfo.parameters’. So to access our parameters we simply have to write,
trace(root.loaderInfo.parameters.par1);
trace(root.loaderInfo.parameters.par2);
NB:
If the SWF file is called directly by the browser the parameters can be passed in the traditional way that they are passed to the script files (url embeded)
eg : http://yourdomain.com/flashfile.swf?par1=hello&par2=world