Subscribe to get my new tutorials in your inbox.

Pass parameters from HTML to Flex/Flash: Dynamic video player example

A quick example of passing parameters from HTML/Javascript dynamically to Flex/Action Script or Flash in some cases using flashvars. You can use it with any Flash based platform. Check out the demo first and then I will explain:

Demo Link:

What we have here is that there are two buttons in an HTML page. Each button when clicked plays a different video in the same Flex Video Player (which runs in a .swf file and uses the Flash plugin to render). So what I am doing is that on click of the button I call a javascript function wherein I pass the corresponding video URL as parameter to the Flex Video Player (which is a .swf file) using flashvars. This is how I am doing it,

<param name="flashvars" value="videoURL=videos/video' + + '.flv" /> is the id of the button clicked. The parameter is passed as a key-value pair. videoURL holds the dynamic URL of the video. There is one flash file (.swf) which reads the parameter passed in flashvars and then plays the video. This way multiple videos play in the same player.  I have named the videos as video1.flv and video2.flv which are inside videos folder under my root directory. Note that Flex/Flash video player runs a .flv format. When you look at the demo you might notice that a popup shows the video URL inside the player. That’s where it is getting the URL as parameter from HTML side. Also the video is auto played.
Below is the full HTML/JavaScript code, the index.html file

<input type="button" value="Play Video 1" id="1" onclick="playVideo(this)"/>
<input type="button" value="Play Video 2" id="2" onclick="playVideo(this)"/>
<div style="background-color:#ccc; width:520px; height:500px; margin-top:20px;" id="playHere"></div>

<script type="text/javascript">
var htmlContent;
function playVideo(target_obj) {
htmlContent = '<div style="width:520px; height:500px; text-align:center;">' +
'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="510" height="490" id="videoPlayer"> <param name="movie" value="videoPlayer.swf" /><param name="flashvars" value="videoURL=videos/video' + + '.flv" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="videoPlayer.swf" width="510" height="490"><param name="flashvars" value="videoURL=videos/video' + + '.flv" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--<![endif]--> <!--[if gte IE 6]>--> <p> Either scripts and active content are not permitted to run or Adobe Flash Player version 10.0.0 or greater is not installed. </p> <!--<![endif]--> <a href=""> <img src="" alt="Get Adobe Flash Player" /> </a> <!--[if !IE]>--> </object> <!--<![endif]--> </object>' +
document.getElementById("playHere").innerHTML = htmlContent;

Flex code
Now, let’s see how to create the Flex video player.  For that you will need Adobe Flash Builder or open source Flex sdk (version 4.0 onwards) to complile the code and generate the Flex video player as a .swf file. Below is the full Flex code.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx=""
import mx.controls.Alert;
import mx.core.Application;
import mx.core.FlexGlobals;
protected function getHTMLParameter(event:FlexEvent):void
try {
var valueStr:String;
valueStr = FlexGlobals.topLevelApplication.parameters.videoURL;
adPlayer.source = valueStr;;
} catch (error:Error) {'Error in reading video URL');

<s:VideoPlayer autoPlay="false" id="adPlayer" x="5" y="5" width="500" height="480"

Upon creation complete I call the method getHTMLParameter()and this is where the parameter that is coming from HTML side is fetched and read. All the parameters that are passed are stored in the parameters property of the FlexGlobals.topLevelApplication object. Then we can access them by the parameter name. This same action script code can be utilized with Flash also.

valueStr = FlexGlobals.topLevelApplication.parameters.videoURL;

If you are having Adobe Flash Builder then create a Flex project and copy this code to a mxml file (videoPlayer.mxml). After that you just need to export a release version/build of the file and you will get the final .swf flash file in the bin-release folder inside your project folder. The image below shows the step,

Export release build/final swf file.

Why we need to generate a release build of the code is that the release version does not contain debug information and in that way it is much lighter in size. Now, inside the bin-release folder you will find a few files. Copy the videoPlayer.swf (this is the compiled flash file and our video player), playerProductInstall.swf and all of the .swz files (these are needed by the framework at runtime) and paste them in your HTML directory, directly along with the index HTML file that we created earlier. This is how the demo app was build.

Passing multiple parameters
Till now I have talked of passing a single parameter. What if we need to pass multiple parameters? Let’s see how to do that.
In the HTML/JavaScript side you can add multiple parameters in flashvars by separating them with an ampersand (&).  See how I have added a secondParameter variable which is holding a string value of My second value.  Similarly you can add more parameters. Now in the action script code you can access the parameter value by the same method that I discussed earlier.

Java Script

<param name="flashvars" value="videoURL=videos/video' + + '.flv&secondParameter=My second value" />

Action Script

var secondValue:String;
secondValue = FlexGlobals.topLevelApplication.parameters.secondParameter;

In my recent past I have worked extensively with Flex and ActionScript but I will not go much deeper into Flex as this tutorial should only be kept to passing parameters and that’s what we already achieved.
For those who are not sure of Flex, Flash, Video Player, .swf files check out the help links below:

Adobe Flex

Flex Video Player widget

Adobe Flash

SWF File

  1. what is my spirit animal

    Hello just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Safari. I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d post to let you know. The style and design look great though! Hope you get the problem solved soon. Cheers

  2. VincentO

    I have done everything but it’s not just playing when the button is clicked, pls what are my missing? Your quick response will be greatly appreciated.

  3. VincentO

    When i click the Play Video 1 button it shows the player but doesn’t play the video, note i copy your code but if you can send me the full working sample code i will be very happy as it’s important and urgent that i get it to work… my email is: [email protected]
    Thanks once again.

  4. VincentO

    Hi Joseph, thanks for the mail but when i run the index page and click the play video button 1 it only display a dialog box saying: Video URL: videos/video1.flv but doesn’t play the video itself. Please what will be the problem… I use a window 7 – 64bit system, Google Chrome, IExplorer 9. I’ll be expecting your expect assistance. Thanks.

    • joseph

      Put the entire code/folder in a local server or a web server and try to run it. If you have Apache running locally then try to access it by http://localhost/your_folder and it will run. This is happening because of security problems, if you try to run it directly from the folder it does not run.

        • joseph

          I guess running it from the local folder will give security troubles. You can put it in your local server and display it from there. That’s a possibility?


  5. crkpillai

    Thank for the detailed note, I am having a problem.
    I have total of 22 .flv videos in my root directory, when the page is loaded, the news.flv file is loaded. i want this same player to be used for another 21 filles. i have given these just below the player as links, how to dynamically link these videos ?
    [email protected]

    • joseph


      Yes you can do that. If you see my example I pass the video URL as a parameter to the Flash/Flex code. Similarly you have to pass the url as a parameter. Moreover you need to handle that in some event, so pass the url as a parameter in a button click or may be a drop down select. Let me know if you have problems.

  6. Dion Peixoto

    A good online video player can make a huge difference in the reception of your website. Visitors want to watch videos on a full-featured online video platform that blends seamlessly into the look and feel of the site. A full-featured video player that works every time offers opportunities to build and maintain relationships with customers and encourages them to purchase from your site. In addition, it attracts leads and better ads, ultimately generating higher profits.So how can you judge if the video platform and service provider you’re considering is the right one? One thing you should always look for is a custom video player with customizable features. ,

    Look at the best and newest blog post on our very own blog page

Write a Comment

Your email address will not be published. Required fields are marked *