Tutorials Home
What's New
Top Rated
Submit
myTutes
Random!
Flash Sound for Dynamic Sites
Author: Gonzalo Alustiza
This is the code for the function.
function sound (netscape_event,play){
var browser = initialCheck();
var soundTag = ''; |
The variable 'browser' is an array that
holds values returned by the function initialCheck(). The function initialCheck detects browsers and languages. I
will provide it in a 'clipped' version in the downloadable source code.
The only thing that matters here is that browser[1]
holds 'IE' if it is Internet Explorer,'NE' if it is Netscape and 'NN' if other.
Use your favorite function to detect the browser. The
important thing is that the code should be branched for IE or NE.
The var 'soundTag' will hold the text that references
the name of the Movie Clip |
| var reg = /\W/; |
The var 'reg' stores a
Regular Expression. This regular expression will help parse the URL of the HTML element
that called the function.
Remember that the syntax of /\W/ means a
range of any non-alphanumerical character with the exception of the underscore. |
| if (browser[1]=='NE')soundTag =
netscape_event.target.toString(); if
(browser[1]=='IE'&&window.event.srcElement.getAttribute('pathname')!=null){
soundTag = window.event.srcElement.getAttribute('pathname');
}
else if(browser[1]=='IE'){
soundTag = window.event.srcElement.parentElement.getAttribute('pathname');
}
soundTag=soundTag.split(reg); |
Here we get to our first
branch. Depending on which browser is being used we will retrieve the HREF attribute of
the <A> or <IMG> tag that fired this function in different ways.
In case of Netscape we will use the
TARGET property of the EVENT object to achieve this.
If it is Internet Explorer we will use the EVENT
object too, but due to events 'bubbling up', the syntax is a bit more complex.
We use the split function (from the string object)
plus the Regular Expression to parse the URL and split it every time we find a
non-alphanumeric character. Here's an example:
original URL: www.mysite.com/web/mypage.htm
after splitting the string we get an array that looks
like 0=www 1=mysite 2=com 3=web 4=mypage 5=htm The length for this array is 6 so, we will
use array.length-2 so we get at array[4] which equals 'mypage'. This array element is the
tag that has the same value as the name of one of the Movie Clips inside the Flash Movie |
if (arguments[1]=='true'){
if(browser[1]=='NE'&&top.soundtown.document.embeds.misonido.PercentLoaded()!=0){
top.soundtown.document.embeds.misonido.TPlay(soundTag[soundTag.length-2])
}
if (browser[1]=='IE'&&parent.soundtown.document.misonido.PercentLoaded()!=0){
parent.frames['soundtown'].document.misonido.TPlay(soundTag[soundTag.length-2]);
}} |
If the array arguments[1] (the arguments
of the function) equals 'true' it means that the sound should play.
Notice that we are checking the browser and whether
the Flash Movie loaded at least 1%. This is a good place to insert error handling code
since these checks are useless if the invisible frame cannot load its page. Also notice that to access the Flash Embed you have to use the EMBEDS array
in Netscape or the name of the embed in IE.
PercentLoaded () is a method of the Flash Player that
returns a number indicating the percentage of the movie that has been downloaded so far.
If the movie loaded at least 1% it means that no problems arose while embedding the movie.
This does not mean that the movie is ready to play, so you could
check if the movie is 100% loaded before trying to play it.
TPlay (target as String) is a method of the Flash
Player that lets you play a Movie Clip by passing the method a string with the name of the
target (Movie Clip). See how the method gets its target name from the
URL already parsed. |
| if (arguments[1]!='true'){ if
(browser[1]=='NE'&& top.soundtown.document.embeds.misonido.PercentLoaded()!=0){
top.soundtown.document.embeds.misonido.TStopPlay(etiqueta[etiqueta.length-2]);
top.soundtown.document.embeds.misonido.TGotoFrame(etiqueta[etiqueta.length-2],1); }
if
(browser[1]=='IE'&&parent.soundtown.document.misonido.PercentLoaded()!=0){
parent.frames['soundtown'].document.misonido.TStopPlay(etiqueta[etiqueta.length-2]);
parent.frames['soundtown'].document.misonido.TGotoFrame(etiqueta[etiqueta.length-2],1);
} }
return; } |
You can call the function using the play
flag as an empty string and be sure that it will be assumed by the function to be a Stop
order. The TStopPlay(target as
String) method instructs the player to stop playing a Movie Clip, accepting a string as
the Movie Clip name.
After stopping the player we go to Frame #1 using
the TGotoFrame (target as String, frame as Integer) method which
accepts a string as a Movie Clip name and an integer as a frame number. The TGotoFrame method makes sure that every time we play the Movie Clip it
will start from the beginning. |
As you can see the code is pretty straightforward. In the next
section we will see how to call this function from HTML using event handlers.
| » Level Intermediate |
|
|
Added: : 2001-02-20
Rating: 6.34 Votes: 29
Hits: 4685
|
| » Author |
|
My name is Gonzalo Alustiza and I work as a chief developer at Soundtown
Argentina. Soundtown is a ten year old music production company
specializing in music for advertising and TV which started to develop
sonification solutions for the Internet.
|
| » Download |
|
Download the files used in this tutorial.
|
|
Download (89 kb)
|
|
Get conversion and unzipping tools
for PC and Mac here!
|
| » Forums |
|
More help? Search our boards for quick answers!
|
|
Please rate this tutorial, 10 is the top rating, you can also click the
comments link to read/write a review.
|
|
|