How to Use Flash MX Sound Objects

This document copyright 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com
Passing Variables from HTML to Flash to Select which MP3 will Play Return to Index Do you want all FLA's used for this tutorial? Do you have a question?
fig.30, This player loads external MP3's based on file names that are inserted as variables in the object and embed tags of the HTML for this page.
Special thanks to the musical generosity of JiXeR for providing the tunes used in this example. To purchase the elements album by Jixer, visit www.JiXeR.org.

The following will tell you how to edit the object and embed tags of your HTML to pass variables (file names, in this case) to the Flash player using the FlashVars property of the Flash player. In this way, you can edit the HTML file (versus your Flash) to update your playlist. Variables that are created in the HTML file will be available before the first frame of the flash movie, and these variables are accessed on the _root level.

For example, if you create a variable in the object and embed tags of the HTML like mySong1 = AmericanDream.mp3, then in your Flash movie, _root.mySong1 is equivalent to AmericanDream.mp3.

When you select FILE then PUBLISH from Flash, it creates an HTML file with an object tag and an embed tag in the HTML. The following is an example of the object and embed tags within the HTML file for a Flash movie called passingVariables.swf.

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="400" height="30" id="passingVariables" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="passingVariables.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />


<embed src="passingVariables.swf" quality="high" bgcolor="#ffffff" width="400" height="30" name="passingVariables" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>

In order to pass variables to the Flash movie from the HTML tags, and make it compatible across Mac and PC and multiple browsers, both the embed and the object tags must contain the variables. The following is an example of using the FlashVars property in the object and embed tags:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="400" height="30" id="passingVariables" align="middle">
<PARAM NAME=FlashVars VALUE="mySong1=AmericanDream.mp3&mySong2=FewFarBetween.mp3&mySong3=Rain.mp3">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="passingVariables.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />


<embed src="passingVariables.swf" FlashVars="mySong1=AmericanDream.mp3&mySong2=FewFarBetween.mp3&mySong3=Rain.mp3" quality="high" bgcolor="#ffffff" width="400" height="30" name="passingVariables" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>

FlashVars is bolded for emphasis only. Note that in the object tag, the FlashVars property is preceded by "<PARAM NAME=" and followed by "VALUE", but it is not in the embed tag.

In the above example, three variables have been established: mySong1, mySong2 and mySong3. When declaring more than one variable, each variable is separated with the "&" character. If you need to insert special characters (such as a string), proceed the special character with "%".

In the above example, mySong2 equals FewFarBetween.mp3. In order to load that MP3 as a streaming sound on button press in the Flash player, the following would be used.

on(press) {

_root.mySound2 = new Sound();
_root.mySound2.loadSound(_root.mySong2, true);

}

Note: As a reminder, dynamic streaming sounds (like above) do not need a start command (e.g., _root.mySound2.start();). Streaming sounds will start as soon as they have enough data to start.

In the example used on this page, the MP3 ID3 tag data is used as scrolling text. Therefore, even the scrolling text does not need to be updated inside the Flash movie. However, the content (text string) of the scrolling text could also have been a variable passed from the object and embed tags. This is a nice solution for the client who cannot edit your Flash files after being delivered.

 

Previous Next
How to Troubleshoot Sound Objects END
Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com

This section of the tutorial was added on: November 11, 2004.