How to Tell a SWF What File(s) to Load — From the Outside

Flash ActionScript 2.0

A number of people have asked how I handled the microphone icons in my last post, Papi’s Wah-feh (An Audio Guide).  Did I simply use Save As a bunch of times to create eight separate SWFs, each with its own imported audio?  I certainly could have done that, but I decided instead to create one single SWF that could be told from the outside which MP3 to play.  Not only does this mean I can re-use that SWF as often as I like — without recompiling, by the way! — but it also reduces download time, because the SWF is only retrieved once (only 407 bytes, at that!), and the MP3s only load as needed.

The following technique can be used for graphics, too — really, for any file a SWF can load at runtime, from CSS to video (FLV), to other SWFs.  Let’s take a look. 

An answer, moderately long and sweet (and how it works)

It’s a good idea in general to load external files for audio, unless you need tight synchronization, such as for cartoon lipsynching.  Why?  Well, again, a SWF by itself has a much smaller footprint without the extra weight of imported sound files.  Besides, if the user happens not to venture down a particular path in your movie, why force the download for that section?  Let the user download files as they’re needed.  ;)

Under normal circumstances, I might have programmed the microphone graphic (a button symbol) with my ActionScript in a keyframe like this:

var s:Sound = new Sound();
iconButton.onRelease = function():Void {
 s.loadAudio("path/to/audio/sound.mp3", true);
}

Note:  For a look at this code in the AS3, see “How to Retrieve FlashVars Data in ActionScript 3.0.”

In the first line, an arbitrarily named variable, s, is declared and set to an instance of the Sound class.  In line 2, a function literal is assigned to the Button.onRelease event of a button symbol whose instance name is iconButton.  This function literal calls the Sound.loadSound() method on the s instance and supplies the path to an external MP3 file.  The second parameter, true, tells Flash to start playing the audio as soon as enough data are available — even if the file isn’t yet completely downloaded.

But clearly, this approach is hard coded.  In order to open up the possibilities, replace the path reference with a variable — let’s just call it audio.

var s:Sound = new Sound();
iconButton.onRelease = function():Void {
 s.loadAudio(audio, true);
}

Note, the quotation marks are gone, because the audio variable now contains that string.  But wait, where is this variable declared?  Currently, it’s undefined, right?  Right.  We’re going to pass in the desired string from outside the SWF.

There are a number of ways to do this, but in this article, we’ll look at the FlashVars approach.  SWF files are embedded into HTML via the <object> and <embed> elements.  The <object> element contains several child <param> elements, and these contain attributes that set various properties of the SWF, such as where the SWF file is located, its width and height, quality settings, and so forth.  Internet Explorer reads the <object> element and other browsers read <embed>, so if you look carefully at a working sample, you’ll notice that <object>’s <param> attributes are basically mirrored in the <embed> attributes.  With FlashVars, you simply need one additional <param> element and one additional <embed> attribute to match, both of which can be tossed into the mix wherever you please.

Here’s the skeleton of a typical <object>/<embed> pair.

<object [several attributes, including width and height] >
 <param name="movie" value="movie.swf" />
 <embed [attributes, including width, height, and src]></embed>
</object>

FlashVars effectively allow you to declare a SWF’s variables from the HTML.  Note:  these will be strings only, so if you want to pass in a number, you’ll have to cast it as such inside the SWF (parseInt() or parseFloat(), for example).  These variables will be scoped to the main timeline, so using FlashVars is the equivalent of typing …

var audio:String = "path/to/file/audo.mp3";

… in frame 1 of the main timeline.  Here it is.

<object [attributes] >
 <param name="movie" value="movie.swf" />
 <param name="FlashVars" value="audio=path/to/file/audio.mp3" />
 <embed [attributes] FlashVars="audio=path/to/file/audio.mp3"></embed>
</object>

Again, note that both the <param> and <embed> elements need the same information.  The above creates a string variable audio in the main timeline — which is the variable reference in the Button.onRelease handler above!  So there it is.

If you want to pass in a number of variables, separate each name/value pair with an ampersand (&).

audio1=aaa.mp3&audio2=bbb.mp3&audio3=ccc.mp3

What about SWFObject?

If you’re using Geoff Stearns’ SWFObject to get around the IE “click to activate” issue, simply use his object’s addParam() method to add your FlashVars virtually.

<script type="text/javascript">
 var so = new SWFObject(
  "movie.swf", "mymovie", "200",
  "100%", "7", "#336699"
 );
 so.addParam("FlashVars", "audio=music.mp3");
 so.write("flashcontent");
</script>

Update!  Here‘s a sample file (thanks for the suggestion, Amy!) to illustrate what‘s shown above.  The sample shows an MP3 file specified from the HTML, using traditional embed and SWFObject.  The SWFObject example uses addVariable() instead of addParam(), as shown above.

91 Responses to “How to Tell a SWF What File(s) to Load — From the Outside”

  1. ImagicDigital Says:

    Also, SWFObject has a custom method called addVariable() for adding FlashVars, so you could also easily add several variables like this:

    so.addVariable(”audio1″, “music1.mp3″);
    so.addVariable(”audio2″, “music2.mp3″);
    so.addVariable(”audio3″, “music3.mp3″);

  2. David Stiller Says:

    Good point, ImagicDigital! I’m so used to FlashVars that I tend to overlook that feature of SWFObject. Thanks for bringing it up. :) I think that approach may “gel” better for some people.

  3. Bert Says:

    Hi David,

    Great site.

    Total newbie to Flash 8, but learning quickly.

    Let’s say we have an html file where player.swf file is embedded (linked to 1 flv in the same folder via contentpath in flvplayback component). I have several flv’s in the same folder that i want to see using the same swf player. How can I code for changing the `contentpath’ in the swf without republishing? Can I change the contentpath of the swf by editing the html alone?

    If this does not make sense, dont shoot the messenger lol.

    Thx.

  4. David Stiller Says:

    Bert,

    Your question makes sense. No need to shoot anyone. ;) The answer to your question is spelled out in the article above. Just think to yourself: how would I set the contentPath property by hand? — I assume you’re using the FLVPlayback Component? — and when you get that squared away, just replace the hard-coded file path with a variable, and feed in the variable as described.

    For example, give your FLVPlayback Component instance, on the Stage, the instance name video (or whatever you like). Don’t use the contentPath parameter in the Component Inspector, but rather, in a keyframe type video.contentPath = "sample.flv";. It’s really that simple. Once you test that proof of concept, change the string “sample.flv” to a variable and set that variable from your HTML.

  5. Bert Says:

    Wonderful,

    This is the missing step. When I said I was a newbie, I lied. I only started Flash and HTML yesterday :)

    Many thanks, this will get me started on my project.

    kl.

  6. senan Says:

    Hi David - My question pertains to the above q. regarding ‘How to Tell a SWF What File(s) to Load — From the Outside’. My issue is similar…but different. I’ve got ‘main.swf’ that loads various .swfs from within the same directory. One of those .swfs is a video page that has a FLVplayback component that is ‘bound’ to a comboBox that calls 19 different 2 minute videos. I followed a well written tutorial I found and when I test the video .swf by itself, it works beautifully. However, when testing from the ‘main.swf’, the video.swf shows up, as well as the first video in the comboBox, but does not allow access to any of the other videos. No menu drop down is available now. WHERE DID I GO WRONG??!! I’ve tried everything I can think of — so any help/insight is greatly appreciated. Thanks so much!

    Senan

  7. David Stiller Says:

    senan,

    Similar but different, eh? ;)

    I’m glad to help when I can, but in this case, the possibilities are pretty wide open. You’ve described quite a bit without going into any details. To find the exact place you went wrong (or possibly the many places), you’ll have to narrow things down. This calls for classic troubleshooting. It sounds like you should start with the video SWF, since the others seem to be working. Embed the video SWF in an HTML page and check if it works while running in the browser on its own. Start a new FLA and use that to load the video SWF in its own, and so on. As it is, you’re stuck with too many places to look. Clear the fog. ;)

  8. senan Says:

    david - I much appreciate the feedback and will begin packing for my troubleshooting journey. Wish me luck - bonvoyage - will keep you posted. Thanks again!

    auf Wiedersehen! : )

  9. senan Says:

    Hmmm - Getting the same results. From the standalone video test page, I’ve uploaded the 1st 3 videos and they work nicely - however, calling it from the new .fla/swf embedded in an html page, no dice - nor from the ‘main.swf’ — Here are a few links to show you what’s going on. Again, I appreciate any help on this - overall, it’s for a good cause —

    – Player on its own:
    http://www.northpoledesign.com/nsp_application/nspvideopagetest1.htm

    – New .fla created to call vid page:
    http://www.northpoledesign.com/nsp_application/vidtest1.htm

    Here’s the code I’m using to call the video page from the video button inside the ‘main.swf’:
    on (release) {
    empty.loadMovie(”nsp_videopage.swf”, 1);
    }

    Is it something to do with the contentPath and needing to AS it vs. use the component inspector? Thanks David!!

  10. David Stiller Says:

    senan,

    I can only guess that you’re using paths that don’t translate well when the working SWF is loaded into another. Maybe you’re referencing _root in your paths? That’s a gotcha (explained here). Given the complexity of your technique, here — ComboBox databinding, loading into another SWF — the error (or errors) could be in a number of places. Have you experimented with the Debugger panel or other Output panel debugging approaches? You need to “see what’s under the hood,” so to speak.

  11. Robert Says:

    How about passing the volume level to a swf to control the volume of an flv using SWFObject? Possible? SOmething special needed in the swf file to account for it or can it be controled through a flash var that is inhereant with flashscript???

  12. David Stiller Says:

    Robert,

    Volume shouldn’t be a problem — in fact, I can’t think of an example that couldn’t somehow be handled through FlashVars, even by way of SWFObject. In addition to any other name/value pairs you might have, just include a variable for volume. If you like, call it something like extVolume (for “external volume”), and whenever you would have hard coded a number to set the FLVPlayback component’s volume (or whatever other approach you’re using), use the variable instead.

  13. Seth Says:

    Hello,

    I am an absolute noob when it comes to flash and I am totally lost. I am trying to have my swf load a FLV, but I want only to have 1 swf file. Basically I want to have one swf load a FLV file based upon the URL (ex: player.php?video=somevideo) and have it play that file and when i change it to player.php?video=anothervideo have it play that. So far everything I’ve tried has failed and I’m at an absolute loss. Please help

  14. David Stiller Says:

    Seth,

    Your question is actually answered on the trail between the original post and some of the other comments. The post covers a general approach to getting outside variables — such as from a URL — into the SWF. Once in the SWF, you can use the variable to load whatever you like. Where are you getting stuck?

  15. Seth Says:

    Im getting stuck on the whole process of actionscript and associating it with php to load the video.

  16. David Stiller Says:

    Seth,

    There are a number of ways you could go. You could use PHP to write out the FlashVars attribute of the <object><param> / <embed> HTML tags — that seems like a straightforward way to go.

    Check out this info-packed tutorial: Passing Variables from HTML to Flash via FlashVars. It shows you how to grab data from a query string and get it into Flash (example #3).

  17. Seth Says:

    Not working for me…
    This is what I have:
    PHP:

    .flv”>


    bgcolor=#99CC33 WIDTH=488 HEIGHT=390
    TYPE=”application/x-shockwave-flash”>

    ActionScript:

    onClipEvent (load) {
    loadMovie(_video.file, “”);
    }

    Im still at a loss, if it helps in using Proxus FLV Player 2.0.1 sorry, i don’t know any action script what so ever…

  18. Seth Says:

    It didnt show up…. goto: http://www.ani-stream.com/uploads/player.php

  19. David Stiller Says:

    Seth,

    Bro, I’m at something of a loss here, too. ;) If you don’t know any ActionScript whatsoever, I can only recommend that you start from scratch and learn the basics. I wouldn’t try to build a cabinet for my dining room until I knew a bit about carpentry. You know?

  20. Seth Says:

    Well, I do know php fairly well. Its just that I want to change my contentPath (in my component its called: _video i think) and ive tried going about his in every way shape and form and no luck towards it. Is there any referece you can give me? Or in actuality can i give you the files to look at? I’ve yet to find somebody that could help…

  21. David Stiller Says:

    Seth,

    Here’s something that works for me: when I’m faced with a goal that just stands in my way — something that seems insurmountable to me — I try to break it down into smaller pieces. In a case like this, I would probably save my work and start a brand new FLA, without the “clutter” or “distraction” of the Video object, that Proxus player, or anything else. I would focus on simply getting a variable into my SWF from the outside. Start without any external programming at all: start by adding a dynamic text field to your FLA, giving it an instance name, and setting its TextField.text property to a string.

    // in a frame script
    myTextField.txt = "lorem ipsum";

    With me so far, right? It doesn’t get any more basic than that. Now, change that literal string to a variable:

    // in a frame script
    var myStr:String = "lorem ipsum";
    myTextField.txt = myStr;

    Each one of these small steps is an encouragement. Next, move that variable from inside the SWF to a FlashVars attribute in the HTML:

    // in a frame script
    myTextField.txt = myStr;
    // in HTML
    <object [attributes] >
     <param name="movie" value="movie.swf" />
     <param name="FlashVars" value="myStr=lorem%20ipsum" />
     <embed [attributes] FlashVars="myStr=lorem%20ipsum"></embed>
    </object>

    Still with me? At this point, keep going. Instead of hard coding the FlashVars part, use PHP to write the value after the equals sign. After that, set some other property of the text field (see the TextField class in the ActionScript 2.0 Language Reference for available properties). Just keep in mind that any data you pass in are strings, so you might need to convert or cast to other datatypes. Eventually, all these baby steps lead you toward setting the necessary property of the Proxus player (which I haven’t heard of).

    I did give you a reference. :) I pointed you to an external tutorial that shows you several ways to get variables into a SWF (even with PHP). I’m here to help, but I strongly believe in helping people help themselves.

  22. Markus Says:

    Hello,

    I have been looking thru these threads - every little bit of information is helpful - however I am unable to find anywhere if you can after loading in Flashvars can you reference them via an array type senario like you can args in a js function … arguments[0] … hopefully something like FlashVars[0] … does anyone know of the syntax I could use to achieve this. I am stuck with a dynamic string where I do not know how many paired params will be supplied.

    Thank you in advance

    Markus

  23. David Stiller Says:

    Markus,

    Use a for..in loop to run through the main timeline — that’s your best bet. All FlashVars variables are strings, and they all appear in the main timeline.

    for (var prop:String in _root) {
      trace(prop); // shows variable name
      trace(_root[prop]); // shows variable value
    }

    The array access operator, [], allows you to reference these objects by strings. Use in cahoots with the typeof operator to make sure the dynamically reference objects are strings, and not other variables (of other datatypes, perhaps) that you declared internally.

  24. Dax Castro Says:

    Okay.. So.. I too have looked through all the code and I think I am okay up to a point. Nothing really addressed the issue the way I am using it. I can see how to reuse the parameters to utilize the same swf to call different videos/files, but there wasn’t anything in here about how to tell the swf what flv to load from a hyperlink.

    Take a look at my page. http://0160b7d.netsolhost.com/masstort.html

    Inside the swf, I followed your methods and have (on its own layer)

    1 videoPlayer.contentPath = video;

    when I change this to mass05.flv it does play the video so I know that I was on the right track

    so video is my variable…right?

    so now I want to have the user click on any of the links and have the swf be told what flv to load.

    So call me clueless, but I am just not seeing how to do this in any of the posts above.

    Thanks for your help. You really seem to be giving people a real hand.

  25. David Stiller Says:

    Dax,

    when I change this to mass05.flv it does play the video so I know that I was on the right track

    so video is my variable…right?

    By the looks of it, that’s right. Your variable seems to be video. If that’s the case, you need to provide that variable a value, otherwise it’s just a variable that doesn’t mean anything. The way in which set the value of a variable is up to you, of course. ;) In the context of this article, it would occur by way of a FlashVars name/value pair. In your HTML, you would set the name video to the value mass05.flv. Have you done that yet? I ask, because it looks like your hyperlinks are linking to SWFs rather than HTML documents that embed SWFs. Without the parent HTML document, you’d have to feed your variable in with a query string.

  26. Ben Fleming Says:

    Thankyou so much! I’ve been googling how to do this for a while now, but I only get answers to getting the variables from .html links. This code works perfectly :)

  27. Ben Fleming Says:

    Sorry for the double-reply, but I have a problem. I have a hyperlink in Flash, using the getURL method, and I link directly to a .swf file. Is it possible to add variables onto that link? I’ve tried it normally, but it didn’t work. Any suggestions?

  28. David Stiller Says:

    Ben,

    You can indeed pass variables into a directly linked SWF. Rather than FlashVars, which would of course require HTML, just append a query string after your SWF reference.

    <a href="myMovie.swf?myVar=string%20here">Flash!</a>

  29. osman Says:

    I have a main swf.I want to see my other swf file by clicking the button but not in my main swf.thanks for your help.

  30. David Stiller Says:

    osman,

    You have a main SWF. You want to see another SWF by clicking on “the button” — where is this button?

  31. Camaban » Blog Archive Sticking Flash on you web site » :The work and pleasure of a mad, fictional, pre-historic monument builder Says:

    […] There’s theres the basis of the information needed about passing the value of an flv file into the player (so you have your flash video player, and just tell it which video to play) at quip.net. […]

  32. G Says:

    Hi David,

    I will first say thank you for your help so far. I managed to get a file loading using FlashVars embedded in my webpage with your help.

    However, it seems that it only works when the flash file, HTML file & flash video are all in the same directory. As soon as I try and populate a web address into the embed code (either referencing the SWF or FLV in a folder) the video stops working.

    Is there any chance you could point me in the right direction as I am completely lost with this flash stuff.

    Thanks in advance.

    G

  33. David Stiller Says:

    G,

    There’s a bit of a trick to file paths when it comes to SWFs and relative file paths. You can either use absolute paths (fully qualified domain name preceding each file) or relative, but if you use relative paths, be aware that the SWF’s “point of view” isn’t its own location, but that of the HTML file that holds it. With the exception of FLV files — for those, the point of view is relative to the SWF.

    So, if you’re loading the FLVPlayback Component, for example, and you want to specify a skin, use a file path relative to the HTML document (because skins are SWFs), but when you’re specifying the FLV, use a file path relative to to the SWF that contains the FLVPlayback instance.

  34. Amy L Says:

    Hi David-

    Is there a sample flash file we can view? I’ve used Flash before but it was mainly for basic animations and VERY basic actionscripting like button states and what not.

    My only problem with this whole thing is, I’m not sure where all the codes is suppose to go in the flash file and what codes needs to be in there. I’ve been searching the net but nothing close to helping me except your article here! =)

    Thanks,
    Amy

  35. David Stiller Says:

    Amy,

    Once you see it all in place, I think it’ll gel for you right away. See the very bottom of the original article for a sample file.

  36. Matt Says:

    Greetings,
    First off I just wanted to say thanks for all the useful information you’ve posted through out your sites, I’ve learned a lot. So I’ve built a video player ala, gotoandlearn.com, and I want to control what FLV’s are playing by using HTML links, much like previous posts here, but I’m not using any components.

    So I have player.swf playing an external FLV using NetStream and the player is in an HTML page. An inital video is set to play
    ns.play(”video1.flv”);
    I want to have three text links on the page that tell player.swf to play three different movies. I tried a few different methods but I can’t seem to get it all to work. The latest experiment involved the PassFlash function in JavaScript were I would send a variable from HTML to a dynamic text box in player.swf. I added a textfield listener and setup a onChanged function to just play ns.play(”video2.flv”);
    So when I test the page and click on the link it sends the variable, “2″ to player.swf and it appears in the text box but the video2.flv does not start. Now if I change the text box to an Input Text type box and test the page I can type a “2″ in the box and the second video will play. I don’t know if PassFlash will really work for what I’m trying to do or if I need to use FlashVars or something else but any guidance would be appreciated.

    Thanks,
    Matt

  37. Steven Says:

    Hi -

    Is there a way to use a hyperlink containing only an URL to open an FLV player in a popup window? Thanks.

  38. David Stiller Says:

    To Matt …

    I’ve never heard of PassFlash, so I’m guessing that’s somebody’s API for handling data transfer between a SWF and outside JavaScript. If that’s what you’re after — “live” updates via hyperlinks — you may want to check out the ExternalInterface class. That lets you trigger completely custom ActionScript functions from JavaScript any time you like. I’ve added that to my to-do list for this blog.

    Your TextField.onChanged approach isn’t a bad idea, but check out the ActionScript 2.0 Language Reference for the entry on that event:

    The onChanged handler is called only when the change results from user interaction; for example, when the user is typing something on the keyboard, changing something in the text field using the mouse, or selecting a menu item. Programmatic changes to the text field do not trigger the onChanged event because the code recognizes changes that are made to the text field. (Emphasis mine.)

    To Steven …

    If your hyperlink contains a query string with name/value pairs and directly opens a SWF file, those variables will be place in the SWF’s main timeline. If the same hyperlink opens a new HTML window, you’ll have to acquire those variables from the loaded page. The JavaScript search property, described here at DevGuru, will do that for you. I’ll add your question to my to-do list as well. :)

  39. Kevin Says:

    David,

    As you can see, I’m making the most of your blog. OK.Two more questions:

    1. can you use FlashVars to pass a boolean value? If so, what is the syntax of that?

    2. What I can’t figure out anywhere (and I’m sure I just haven’t looked hard enough) is what the url that contains multiple variables looks like? In my case, it would include the variables: VIDCHOICE, VIDMENU & JUMPFRAME(boolean).

    Thanks!

  40. David Stiller Says:

    Kevin,

    As you can see, I’m making the most of your blog.

    I love to hear that, man. :) It feels good to know that my rambling can be helpful.

    1. can you use FlashVars to pass a boolean value?

    Yes. Well, yes and no. In your name/value pair, just use the word “true” or “false” for the value …

    e.g.
    FlashVars="booleanA=true&booleanB=false"

    … but, keep in mind, all FlashVars are actually strings, so you’ll need to treat your “Boolean” data carefully once inside Flash. In your code that checks the value of these strings, use the Boolean() function to convert the strings to actual true/false values. (See A Tip on the Boolean() Function (Casting as Boolean) for some pointers.)

  41. Kevin Says:

    David.

    I can’t possibly have done this right, but what the heck. Maybe I have? And, if by some miracle I have done it right, do I need to use placeholder variables to receive the variables (frameLink & vidLink) or can I just put it right into the variables I already use (vidFrame & vidChoice). I assumed I would need to use temps because I am otherwise initializing the variables in the first frame. Does this make any sense?!

    //This links to the submenu
    var frameLink:String = “”;

    // This is the video they were sent
    var vidLink:String =”";

    //This tells me that they’ve hyperlinked in
    var sentLink:String = “False”;

    var placeHolder:LoadVars = new LoadVars();
    placeHolder.onLoad = function (success:Boolean):Void {
    if (success) {

    //menuChoice, vidName & referred would be in the html
    frameLink = menuChoice;
    vidLink = vidName;
    sentLink = referred;
    }
    }

    var deepLinked:Boolean = (sentLink==”false”) ? false:true;

    //This would tell me that I should jump ahead.
    if (deepLinked) function() {
    vidFrame = x;
    vidChoice = y;
    gotoAndStop([vidFrame]);
    };

  42. Jasmin Says:

    Hi David,

    First, thanx for this blog, that’s very helpful :)

    I would like to know if you ever worked using ASP, because I’m currently working for a programmer that uses ASP. Our problem is that we use SWF Object to load the SWF, but I cannot pass any variable to Flash using the so.addVariable property, and I cannot figure out why..

    Everything works fine if I just save my .asp in a .htm file or if I use the embed tag instead of SWF Object, but since the website we’re working on is huge and we’re only updating it, we cannot simply transfer the code from ASP to HTML.

    Any help would be more than appreciated!

  43. Jasmin Says:

    Problem solved.

    I had to leave a blank keyframe at the beginning of my SWF. It looks like asp processes javascript slower than HTML does, or something like that…

  44. David Stiller Says:

    To Kevin …

    Your sample code uses the LoadVars class, which is certainly a way to load data from outside the SWF, but it’s different in that the SWF is doing the asking, whereas here, the variables are being “forced” on the SWF from the outside as it loads (or arguably, before it loads). Now, you may very well be combining techniques — using FlashVars-supplied variables in cahoots with LoadVars — and if that’s so, you don’t need to “prep” any of your variables, but it may make sense to declare the LoadVars variables outside the onLoad function, which it looks like you’ve done.

    But honestly, you don’t have to do that either, because the placeHolder variable (your LoadVars instance) has the data you want — once those data are loaded. Part of what confuses me here is that you haven’t used any of the LoadVars methods that actually do anything. You’ve instantiated an object (placeHolder), you’ve prepared an onLoad event handler, but you haven’t loaded anything (placeHolder.load() or placeHolder.sendAndLoad()).

    I’m guessing you’ll want your if (deepLinked) statement inside the onLoad event, but I’m not sure. You don’t need to declare a function (after that if) to get the if’s contents to perform. Then, too, what are x and y? And also, watch out for capitalization! You’ve set setLink to “False” but are later checking for “false”. ;)

    To Jasmin …

    Yay! Glad you got it working. I’m not sure I’ve encountered what you saw — that ASP processes JavaScript slower (once the ASP is processed, the browser sees it as plain old HTML) — but success is good, in any case. :)

  45. Craig Says:

    Hi David,

    I’ve been trying to get variable passing to work between HTML and the FLVPlayback component using the SWFObject and I seem to be stuck. I’ve looked at your example and I can’t seem to figure out what I’m doing wrong. I’m wondering if you can help, please?

    Here’s my script HTML code:

    Get the Flash Player to see the Flash Movie.

    var so = new SWFObject(”http://blah.com/WT_Test_CS3.swf”, “MyTube”, “355″, “275″, “9″, “”);
    so.addParam(”allowFullScreen”,”true”);
    so.addParam(”vidPath”, “http://blah.com/005.flv”);
    so.write(”Test”);

    Then the code I’m using in the swf (which I believe is where it’s breaking is:

    import fl.video.*;
    var flvControl = Display; //named instance of FLVPlayback Component
    var flvSource = vidPath;
    flvControl.source = flvSource;

    This is all the code I’m using. This is actionscript 3 on Flash CS3.

    Any glaring easily correctable errors? If I put the direct path to the flv into the flvSource variable, it works fine.

    Thanks, Craig

  46. Jasmin Says:

    @David
    That was an hypothesis, but I’m glad what I said makes sense (or does it?) A friend of mine who is in programming told me it might be possible, but that I shouldn’t bother too much about it as long as it works lol

    If you want to experience what I did, just save any html file you have to .asp and then upload it to an IIS server (or create a local IIS server), make sure your swf uses the variable on the first frame (in my case, it is a variable used to tell the swf if he has to load an introduction animation) and then load your swf with swfObject. I just left the 1st frame blank and moved my AS to the 2nd keyframe and everything worked fine from that time (I found this solution on a forum, it was a suggestion made by the guy who created SWFObject to solve a similar issue with HTML).

    @Craig
    Read my post, it might help you out! :)

  47. David Stiller Says:

    To Craig …

    There’s nothing glaringly wrong with your code. You’re using more lines than you need to — for example, you could simply put Display.source = vidPath — but your three lines aren’t doing anything wrong. Jasmin’s suggestion to leave a blank keyframe at the beginning might be worth a shot, though I couldn’t explain why.

    To make sure your information is indeed getting into the SWF, I would try a few trace() statements …

    e.g.
    trace(vidPath);

    … because if you get undefined or an empty string, you’ll at least know the first obstacle lies elsewhere.

    To Jasmin …

    Sooner or later, I’ll have to give that a shot. Thanks again for the input!

  48. Craig Says:

    Hi Jasmin,
    I’m not sure how your example relates to mine. Am I missing something?

    Thanks, Craig

  49. David Stiller Says:

    Craig,

    I’m think Jasmin is suggesting you try a blank keyframe or two at the beginning of your movie to let the SWF “catch up,” in a sense, with the incoming variable(s). I can’t deny that such a trick might work, but then again, I couldn’t explain why if it did, and I don’t see anything inherently wrong with your code. Have you tried a trace() yet, to see if the variable has truly made its way into your movie?

    In order for the trace() to be visible when you test in an HTML document, you’ll have to use remote debugging, as described in this Adobe article (start with page 4/4 to jump right into it). You might also route that variable to a dynamic text field for testing, if you don’t want to go to the expense of remote debugging.

  50. Craig Says:

    Thanks David! I’ll give the trace() a try using your article and see what happens. Thanks for explaining what Jasmin was referring to and verifying that my code was OK. That’s exactly what I needed to get ‘unstuck’.

    I appreciate your help!

    Craig

  51. David Stiller Says:

    Craig,

    Sure thing! Best of luck. :)

  52. Mark Says:

    Craig,
    Did you ever figure this problem out? I’m having the exact same problem now and I can’t get it. I would appreciate any help.

    Thanks,
    Mark

  53. Carlos Palacio Says:

    Hello David,

    First, THANK YOU! this blog helped me several times. Today my problem is partially solved by this post, but I’m still not sure how to make it happend. I want to call from SWF1 an HTML popup window that holds SWF2 which will load content depending on what the user select in SWF1. I already know how to popup the HTML window using “getURL(”javascript:Launch(’…” The part that I don’t know how is: How to pass the variables from SWF1 to SWF2. You showed in this post how SWF2 will retrive the vars from the HTML, but how SWF1 will put them in the right place of the HTML that holds SWF2 still a mystery for me.

    Thank you.

    Carlos Palacio

  54. David Stiller Says:

    Carlos,

    Ah, that’s a really good question, actually. The concept goes like this: when you just JavaScript to open a new window, you’re ultimately using the window.open() method (native to the JavaScript API). That method accepts a number of parameters, the first of which is tells the browser what HTML document to open. The second allows you to specify a reference to the opened window. The third allows you to specify characteristics of the opened window, such as width, height, position, and so forth.

    It’s the second parameter that matters in this scenario. The reference name you give the new window is essentially equivalent to an instance name in ActionScript: it gives you a way to speak to the new object directly. Using the name you provide, you can reference the new window, then refer to the SWF in that window by way of the id or name attribute given to its <object>/<embed> tags. Once you’ve referenced the SWF itself, you should be able to interact with it via the ExternalInterface class or the approach described on this Adobe link:

    Scripting with Flash 5 (which still works just fine)

    In any case, though, this would make a great blog entry, so I’ve added it to my list. Thanks for the idea, Carlos! I’ll mention your name in the article whenever I get to it.

  55. Huw Waters Says:

    I was wondering how the same thing could be done with flash video. There’s 1 small thing I don’t see to understand. I’m also trying out the full screen function that’s new in flash 9. My code looks like the following:

    import fl.video.*;

    // 1. Video component instance name
    var flvControl = display;

    var f:FLVPlayback = new FLVPlayback();

    flvSource = fl.video.FLVPlayback(fideo, true);

    // 2. Set video parametes
    flvControl.fullScreenButton = fullScrn_btn;
    flvControl.align = VideoAlign.CENTER;
    flvControl.scaleMode = VideoScaleMode.MAINTAIN_ASPECT_RATIO;
    flvControl.source = flvSource;

    Any help, much appreciated.

    Huw

  56. David Stiller Says:

    Huw,

    The principle is the same for video files as it is for any other: a particular loading mechanism, at some point, is going to ask for a file. In terms of the FLVPlayback component, if you’re using ActionScript 3.0, the mechanism is the FLVPlayback.source property, as you’ve shown.

    The trick is that AS3 looks for passed-in variables in a different location from AS2 — which isn’t at all obvious if you don’t know to look for it. Rather than populating variables in the root (main timeline), ActionScript 3.0 SWFs look for their variables in the LoaderInfo instance of the main timeline — specifically, the parameters property of that instance — which may be referenced via the DisplayObject.root property of the main timeline like this:

    flvControl.source = root.loaderInfo.parameters.flvSource;

    Since the main timeline is a DisplayObject, it has access to the DisplayObject.loaderInfo property, which points to the LoaderInfo instance associated with the main timeline (perhaps a bit confusing, but roll with it, if you can). From there. the next part of the expression, parameters.flvSource leads to the flvSource property (your variable) of the parameters property of the previously described expression.

    This complex approach is only necessary in ActionScript 3.0, which you’ll need if you’re after the fullscreen functionality of Flash Player 9.

  57. Artur Santos Says:

    Hi, david great deal with this site. I have a question for you, i´m developing a web site with trendyflash site builder, and everthing its seems to work good, but when i publish the site on my local computer the hyperlinks made inside don´t work i think is because my OS its windows XP SP2 and the browser IE6 sp2 i think its protecting the active content, could please give some tips.

  58. David Stiller Says:

    Artur,

    Gosh, I don’t know how valuable my suggestions may be — I have no personal experience with Trendy Flash! Recent versions of Flash Player feature tighter security restrictions than they used to, so regardless of your browser, you’re likely to see warnings when accessing online content while testing a SWF from your hard drive (unless you’re in the Flash IDE itself; and even then, sometimes). If you upload your SWF to a server and test from there, you should see improvement, because at that point the Flash content is being tested from an online context.

    Internet Explorer, running in Windows XP (Service Pack 2) or higher, refuses to play active content or scripting unless you put something called “the mark of the web” in your HTML document. See this Adobe TechNote for details. This might just be your best bet, because I know how convenient it is to test content locally.

    TechNote 19578

  59. Eduardo Says:

    Hello David, I need major help, How can i put in the variables when I’m using this (custom (FLV) flash player)
    ns.play(”URL”);? please answer my question I really need this. Thanks

  60. David Stiller Says:

    Eduardo,

    First, decide on the name of your variable. If your intent is to play video, you might, for example, name the variable videoFile. This would be the variable that you pass into Flash from the outside. In ActionScript 2.0, a videoFile variable will automatically be declared for you as soon as the SWF loads, so in your call to NetStream.play(), you would supply your variable instead instead of a quoted file path:

    ns.play(videoFile);

    In ActionScript 3.0 — just in case that’s what you’re using — you would retrieve the variable from a different location, as described here:

    How to Retrieve FlashVars Data in ActionScript 3.0

  61. Eduardo Says:

    So I would have to add this –> [ns.play(videoFile);] to this ——>

    [[[ var s:Sound = new Sound();
    iconButton.onRelease = function():Void {
    s.loadAudio(audio, true);
    } ]]]]]

    I’m really confused LOL. And I’m using actionscript 2.0 (flash 8 professional)

  62. Eduardo Says:

    Nevermine I figured out Yay! I’m jumping with joy hahaha this might make my life a lil’ easier now. Thanks so much your tutorials are such great help.

  63. David Stiller Says:

    Eduardo,

    I’m glad to hear that! It sure does feel good when a solution presents itself! :)

  64. Marc Says:

    David-
    I am trying to load a variable using LoadVars from a text file and I want to use this variable in loadSound. The variable loads correctly but will not work inside loadSound. Your help is appreciated.

    var myData = new LoadVars();
    myData.onLoad = function() {
    var s:Sound = new Sound();
    s.loadSound(this.myAudio, true);
    };
    myData.load(”text.txt”);

    My text file looks like this:

    myAudio=radio.mp3

  65. msmuser Says:

    I have a problem with flashvars when using multiple instances of same swf file.

    They all have and also parameters in embed tag.

    Every instance has different value for the variable City.

    I coded actionscript just to output value of the City and on IE,safari,opera,firefox 1.0 it works perfectly but on firefox 2.0 it works only for first instance while the rest outputs “undefined”.

    Here is the url.
    http://site269.mysite4now.com/pegasussi/flashvars.html

    Any help is appriciated

  66. msmuser Says:

    Just discovered the problem.
    One of the add-ons was making this bug.

    Now that this addon is disabled it works perfectly.

  67. David Stiller Says:

    To Marc …

    You’ve scoped your Sound instance inside the LoadVars instance by virtue of declaring the s variable inside the onLoad event handler. That might be what you’re running into. Try declaring s outside the function. When referencing the loaded data from outside the LoadVars instance — such as in a later frame, if that’s what you’re after — you’ll have to use the LoadVars instance (myData) as your prefix instead of this. Does that make sense?

    To msmuser …

    Hey, glad you solved it!

  68. Ed Says:

    Hi David,

    I’m still deciding whether to start with Actionscript 3 or 2. (Newbie) I have Flash CS3 and using all it’s bits and bobs (components) and learning slowly. I want to create a basic vid player using the FLVplayback component, with no skin and pass the streaming vid content to it via FlashVars. I haven’t seen one example of a path, using variables etc, going to a streaming server address, ie RTMP://myserver/myvid.flv. My query is that when pointing to a file the whole thing seems to be wrapped in speech marks. ie “myvariable=../path/to/file.flv”. I can’t get it working. (I only got the example shipped with flash working after someone on a blog said the AC js script had to be ammended also in html. Basically I want to Flashvars flv’s from a FMS streaming server?? Many thanks for your time, will keep plugging away..

  69. David Stiller Says:

    Ed,

    Any variable that comes into Flash is a string, so that explains the quotation marks you’d have to use in your HTML. If you’re using Adobe’s Active Content JavaScript (AC_RunActiveContent.js), then yes, you’ll have to edit the JavaScript code as well as the <param> and <embed> tags in your HTML. You’ll have to add a <param> tag that looks like this:

    <param name="FlashVars" value="myvariable=../path/to/file.flv" />

    You’ll also have to add an attribute to your <param> tag that looks like this:

    <embed [attributes] FlashVars="myvariable=../path/to/file.flv"></embed>

    Finally, you’ll have to add a set of parameters somewhere in the lengthy list that exists inside the JavaScript:

    . . .
    AC_FL_RunContent(
      'codebase', 'http://download.macromedia.com/pub/
    shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
      'width', '550',
      'height', '400',
      . . .
      'FlashVars', 'myvariable=../path/to/file.flv'
      . . .

    Without that change to the JavaScript, you won’t ever get the variable(s), because the JavaScript overrides the <object> and <embed> tags — purposefully so. The complexity of the AC_RunActiveContent.js implementation is enough to keep me away from it, personally. It works perfectly fine, mind you, but I I found SWFObject so much easier on the eyes. ;)

  70. Ed Says:

    Hi David, Everything working now thanks very much!! With regards to AS3, (don’t know if I should put this in this thread?!) when declaring a variable, if you want to have more than one, do you have to write a line for each, ie loaderInfo.parameters.audio, loaderInfo.parameters.video, loaderInfo.parameters.boxheight etc ??

    Many thanks again

  71. David Stiller Says:

    Ed,

    Glad to hear everything works! :) I’m not sure what you mean by writing a line for each variable. In ActionScript 3.0, every variable you add becomes another property of the parameters property of that loaderInfo instance. In ActionScript 2.0, every variable you add because another referenceable variable in the main timeline. I think you’re asking about the HTML/JavaScript itself, and if so, you need to put everything in the same line. If you have more than one variable, each name/value pair goes into the single string that comprises your FlashVars parameter:

    FlashVars="myPath=../path/to/file.flv&myColor=blue&mySound=abc.mp3"

    Each name/value pair is separated by an ampersand (&).

  72. Ed Says:

    Hi David, at the moment I am trying to get to variables into the swf. Using AS3 and in the fla have done the following -

    player.source=root.loaderInfo.parameters.myvideo;
    player.skinBackgroundColor=root.loaderInfo.parameters.ctrlcolour;

    then in the html, entering the strings as you have mentioned. this half seems to work in the sense it takes one parameter, but not the other. In the html AC js part, do you list the string the same as you do for the object and embed part?

    I’ve had both of these parameters working using flashvars, just not together….still plugging..

  73. Ed Says:

    Hi David, I have it working now, must have been a typo! I guess I’m asking in AS3 if you can just specify all the parameters you want to include in one line,
    ie, loaderInfo.parameters.myvideo.myaudio.myboxsize etc

    Thanks you for all your help, and articles, it’s a slow uphill struggle but enjoyable! (I’m still working out the difference between symbols, objects et al!)

  74. David Stiller Says:

    Ed,

    Aha! No, the line you showed …

    loaderInfo.parameters.myvideo.myaudio.myboxsize

    … would theoretically reference the myboxsize property of a myaudio object, which in turn is a property of a myvideo object, which in turn is a property of the parameters property of the loaderInfo object — all in the house that Jack built. Think of these object hierarchies in terms of folders and files on your hard drive. What you’re after is comparable to a series of files in a folder structure that goes loaderInfo/parameters.

  75. Ed Says:

    Hi David,

    Many thanks for your patience, I swear I will leave you alone soon, just to summarise then, I would have to list each variable as so

    In the fla, AS3

    loaderInfo.parameters.myvideo
    loaderInfo.parameters.myaudio
    loaderInfo.parameters.myboxsize

    so I that I can reference them in html?

  76. David Stiller Says:

    Ed,

    No worries. ;)

    What you’ve shown will do it — but you’ll be referencing them from values supplied by HTML, not really referencing them “in” HTML (I just don’t want you to think the communication goes both ways: you can’t read these values back out of the SWF into HTML).

    You’ll reference these on separate lines, like you showed in your 9:04 am post.

  77. Ed Says:

    That’s great, once again many thanks for your patience and for your help, cheers Ed

  78. David Stiller Says:

    Ed,

    You’re sure welcome. :)

  79. rahim Says:

    how can i publish an embedded code contain swf video into flash video..i just have this code :-

    can u show me how to publish this file into flash video. not in website platform. TQ

  80. David Stiller Says:

    rahim,

    I’m afraid I don’t understand what you’re asking. Can you try to rephrase your question?

  81. Brett Says:

    David,

    Thanks heaps for this. I have encountered a rather annoying problem and can’t quite put my finger on the solution (i’m sure it’s an easy one).

    I’m using swfobject and using flashvars for a button to link to a URL defined from the HTML. The code is the same inside flash, outside it looks like:

    so.addParam(”FlashVars”, “variable=http://www.google.com/”);

    This seemed to have worked so I finished the page, then did the same for a different flash file on a different page, and then the buttons started linking to an ‘undefined’ page. Went page to the original page to check code differences and the original page started to link to the ‘undefined’ page as well without touching the code.

    Can’t figure it out!

  82. Brett Says:

    Sorry, I forgot to add, inside flash the button has a simple on (release) getURL statement.

  83. Brett Says:

    I think i fixed it. I had 3 separate parameters for 3 different variables, and didnt read your bit where it mentions multiple variables separated by a “&”.

    Thanks anyway!! :)

  84. David Stiller Says:

    Brett,

    Yes, sounds like you got it! Three variables would be separated with &s, as in …

    variable=http://www.google.com/&var2=value2&var3=value3

  85. Scott Says:

    Maybe I’m missing something but instead of this line:

    var audio:String = “path/to/file/audo.mp3″;

    Shouldn’t it be the following?

    var audio:String = _root.audio;

    Your article seems to miss the main point which I thought was referencing flashvars in ActionScript 2.0.

    Scott

  86. David Stiller Says:

    Scott,

    I think you are missing something — or I wasn’t clear enough (which wouldn’t be the first time!). :) The variable declaration you mentioned is just an example of what FlashVars is doing.

    These variables will be scoped to the main timeline, so using FlashVars is the equivalent of typing …

    var audio:String = "path/to/file/audo.mp3";

    … in frame 1 of the main timeline. [Emphasis added.]

    You wouldn’t need the suggested replacement either. Scratch the var audio:String = _root.audio;. By virtue of the passed-in FlashVars, that audio variable doesn’t need to be declared at all.

    Does that clear it up?

  87. Scott Says:

    Yep, I got it, thanks.

  88. Scott Says:

    I know it’s not really related to the flashvars topic but would you happen to know how to reference the allowscriptaccess parameter in a flash movie with ActionScript 2.0?

    Scott

  89. David Stiller Says:

    Scott,

    That’s communication in the opposite direction — from the SWF out, rather than from the HTML in. If your HTML is formatted as XHTML, you can pass the URL of that very page to the SWF itself, as described here:

    http://www.communitymx.com/abstract.cfm?cid=02395

    There’s more to it than that first article, but the first one (which is free) contains enough information to get you started. From that point, it’s a matter of locating the node that contains your parameter (actually, a tag attribute) and evaluating its value.

  90. Scott Says:

    So there is no easy function you can use to read the value? I only need to read the value and determine if the value is set to always.

    Scott

  91. David Stiller Says:

    Scott,

    Difficulty is a relative term, but I hear what you’re saying. There really isn’t an easy way to read any value in the HTML outside of FlashVars. In a sense, that’s why FlashVars is there. You could certainly use JavaScript to create a FlashVars variable on the fly, passing in the value of the parameter/attribute you’re after.

Leave a Reply