Passing Variables Into Flash using the Querystring

I decided the write this post because I have been getting numerous requests for using a simple media player (Flash) with a dynamic html page. In other words people would like to have a list of videos in html as links and when you click the link the video reference is passed into the media player (Flash) and plays the video. There are a number of ways to accomplish this task but for this example I am going to show how you can have one page pass variables to a second page via the querystring, the second page picks up the variables using swfobject and passes them into the media player.

The example files are setup so that the home.html page has the link with the name value pairs (querystring). When you run the home.html page in a browser and click the link the player.html page with load. If you look at the location field of your browser you should see something like the following:

http://localhost/passingVars/player.html?videourl=rtmp://localhost/vod/sample.flv&videocaption=just%20a%20bit%20of%20text

First notice that this is running on an http server (e.g. http://localhost). Second notice that there is a ? after player.html, the next section contains two name /  value pairs. The first starts with videourl (is the variable) then rtmp://localhost/vod/sample.flv (is the value). Next you will notice a & symbol this is the beginning of the second name / value pair. Finally videocaption is the second variable and just%20a%20bit%20of%20text is the value of that variable. The value is url encoded.

In player.html there is some javascript that picks up the variables and values from the query string and passes them into the Flash file (final.swf). The following captures both the variable name and the value:

if (swfobject.getQueryParamValue(“videourl”)) { flashvars.videourl = swfobject.getQueryParamValue(“videourl”); }


if (swfobject.getQueryParamValue(“videocaption”)) { flashvars.videocaption = swfobject.getQueryParamValue(“videocaption”)

The preceding also adds the variable names and values to to the flashvars object which makes them available to the Flash file. In the Flash file I am able to retrieve the values using the following actionscript:

var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;

if(paramObj.videourl){
msg_txt.appendText(“\t” + “videourl : “+ paramObj.videourl + “\n”);
msg_txt.appendText(“\t” + “videocaption : “+ paramObj.videocaption + “\n”);

Then it is a simple matter to pass the value for the video uri to the FlvPlayback component. You can find and download the example here. The Flash source file is in the assets folder (final.fla). This file will require Flash CS5.

Published by

nhippe

I work for Adobe as an SE in focusing on the Flash Media Server family of products.

4 thoughts on “Passing Variables Into Flash using the Querystring”

  1. Looking for the sample .fla and .html files related to this post, but I get a page not found error when I click on the link.
    Are they available still?
    Thank you for your help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s