473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Loop multiple sounds onClick w/js?

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,
--

-kW-

Jul 20 '05 #1
1 4645
"Knocked Wood" <Kn*********@webtv.net> wrote in message
news:29**************@storefull-2116.public.lawson.webtv.net...
<snip>
<Script Language="JavaScript">
<script type="text/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.

<snip> 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.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Marc | last post by:
Hello, I have a self - submitting form with multiple submits and I want to detect after the submit which button is pressed. Code example: <form name="example" method="post"...
4
by: Dave Parrin-Bull | last post by:
Hi all, I have been asked (nay told!) to do a quiz for a webpage at work, now I can do the basic things like getting the radio boxes up there and assign values to them but here is what they...
1
by: grandeandy | last post by:
question: in the code below, and in general, how would I input multiple functions into the code, and get multiple outputs? basically, I want to be able to add say 2 or 3 of the div's to the link so...
4
by: David | last post by:
Hi everyone, I am trying to stop an image preload sequence by the click of a mouse but have been unsuccessful trying several methods. Imagine this simple script below that loads 50 images to...
6
by: Dave Spencer | last post by:
Hi all, New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the...
4
by: Arnold | last post by:
Hi there, Here's the situation--there is a text field in a form in which students will key in data. On the keypress event, I'd like for different sounds to be played for each character typed,...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
3
Ciary
by: Ciary | last post by:
Hi all, i've got another problem. it's based on the question i asked before about creating a onclick-property using javascript. now what i want to do is create multiple divs with an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.