Loop multiple sounds onClick w/js?

K

Knocked Wood

Hi,
I looked around and can't find anything on this at all and can not get
it to work for IE.
I'm trying to loop multiple sounds on a game, with three unique
variables, when a link is clicked. Something like...

<Script Language="JavaScript">
<!--

function playSound(soundName, loops, timeLength){
document.embeds[soundName].play();
if(loops > 1){
setTimeout("playSound(soundName, loops, timeLength)", timeLength); }
} // End function.

//-->
</Script>

<!-- EMBEDED SOUNDS//-->
<embed src="frog.wav" autostart="false" loop="false" hidden="true"
name="frog">

<embed src="bird.wav" autostart="false" loop="false" hidden="true"
name="bird">

<embed src="rooster.wav" autostart="false" loop="false" hidden="true"
name="rooster">

<!-- LINKS //-->
<a href="javascript:playSound('frog', 3, 4500);">FROG</a>

<a href="javascript:playSound('bird', 1, 3000);">BIRD</a>

<a href="javascript:playSound('rooster', 2, 7500);">ROOSTER</a>

....errrrr, umm, ...or something like that ;-\
There could be a typo above because I'm just going by memory, (which
hasn't been very good lately). Plus I'm on no sleep for quite a while.
But I "think" it might resemble what I've been trying to do?

There's several sounds on the page.
Each has it's own link and variables,
...name (soundName),
...times it will play (loops),
...and sound length (soundLength).

I have been able to get "something like" above (not exactly like example
above) to work on MSN-TV but not for IE on a pc. I'm not a big fan of
sounds on a webpage but in this situation, a game, we're trying to get
it to work but so far unsuccessfully. So I do know my way around JS a
little bit and sounds almost not at all. This problem has been holding
up this page for a while and am truly using comp.lang.javascript here as
a last resort! Any help or guidance at all on this would be very much
appreciated, (more than you'd ever know!), thanks!

BTW;
I've lurked here on and off for years, learned a lot, and really prefer
to find out my own answers, to just work it out myself, but this one
really has me stumped :(

Have a good weekend,
Thanks again,
 
R

Richard Cornford

<Script Language="JavaScript">


This "hide from older browsers" stuff is no longer needed. The browsers
that were young when it was introduced are now so old themselves that
they have all gone out of use.
function playSound(soundName, loops, timeLength){
document.embeds[soundName].play();

That is optimistic. You are assuming that - document.embeds[soundName] -
will resolve to an object and that the object will have a - play -
method. You should be testing these things before trying to use them.
Assumptions kill JavaScripts.
if(loops > 1){
setTimeout("playSound(soundName, loops, timeLength)", timeLength); }

The string provided as the first parameter to setTimeout is evaluated
and executed in the global context and soundName, loops and timeLength
are function parameters so they will be undefined in the global context.
As the values represent a string and two number they can be included in
the setTimeout string as literal:-

setTimeout("playSound(\""+soundName+"\", "+loops+
", "+timeLength+")", timeLength);

However, you have failed to decrement the - loops - parameter so this
code will loop forever.
} // End function.

//-->
</Script>

<!-- EMBEDED SOUNDS//-->
<embed src="frog.wav" autostart="false" loop="false"
hidden="true" name="frog">

<embed src="bird.wav" autostart="false" loop="false"
hidden="true" name="bird">

IE may be happier if this embed element has an ID attribute instead or
(or as well as) a name, but it is difficult to say as embed is not a
valid HTML 4 element.
<a href="javascript:playSound('frog', 3, 4500);">FROG</a>
<snip>

Never use the javascript: pseudo protocol to execute a JavaScript
function as a side effect. Activating an HREF is considered navigation
by web browsers and if the browser gets the impression that you are
navigating away from the current page all bets are off for any activity
on the current page (the browser thinks you have finished with it). An
onclick attribute that properly cancels the navigation specified in the
HREF is the only safe way of triggering a JavaScript function from a
link.
I have been able to get "something like" above (not exactly like
example above) to work on MSN-TV but not for IE on a pc.

No, that - playSound - function would either fail, or if it worked
(given a very faulty ECMA script implementation) it would loop
indefinitely.
I'm not a big fan of sounds on a webpage but in this situation, a game,
we're trying to get it to work but so far unsuccessfully. So I do know
my way around JS a little bit and sounds almost not at all.

I am not a big fan of sounds on web pages either (largely because I am
always either using my computer to listen to something else and don't
appreciate the intrusion, or I am not listening to anything because
silence is what I want so I still don't appreciate the intrusion) so I
haven't scripted sound playing except for a bit of debugging on this
group.

But you have not defined "not working on IE". Do you get error messages
and if so which? It would be better to post the code that works on
MSN-TV instead of your recollections of it and even better to put a
demonstration test page online (it has to be online as nobody else has
your sound files). A demonstration page does not need to include any
more than the sound playing code.

BTW;
I've lurked here on and off for years, learned a lot, and really
prefer to find out my own answers, to just work it out myself, ...
<snip>

And you haven't noticed that way we tell everyone not to use the
javascript: pseudo-protocol? Or did you assume that we do not know what
we are talking about?

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top