Loop multiple sounds onClick w/js?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Knocked Wood

    #1

    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="JavaS cript">
    <!--

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

    //-->
    </Script>

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

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

    <embed src="rooster.wa v" autostart="fals e" loop="false" hidden="true"
    name="rooster">

    <!-- LINKS //-->
    <a href="javascrip t:playSound('fr og', 3, 4500);">FROG</a>

    <a href="javascrip t:playSound('bi rd', 1, 3000);">BIRD</a>

    <a href="javascrip t:playSound('ro oster', 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.javas cript 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-

  • Richard Cornford

    #2
    Re: Loop multiple sounds onClick w/js?

    "Knocked Wood" <KnockedWood@we btv.net> wrote in message
    news:2900-3F59BA16-22@storefull-2116.public.law son.webtv.net.. .
    <snip>[color=blue]
    > <Script Language="JavaS cript">[/color]

    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    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.
    [color=blue]
    >
    > function playSound(sound Name, loops, timeLength){
    > document.embeds[soundName].play();[/color]

    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.
    [color=blue]
    >if(loops > 1){
    >setTimeout("pl aySound(soundNa me, loops, timeLength)", timeLength); }[/color]

    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("pla ySound(\""+soun dName+"\", "+loops+
    ", "+timeLength+") ", timeLength);

    However, you have failed to decrement the - loops - parameter so this
    code will loop forever.
    [color=blue]
    >} // End function.
    >
    > //-->
    > </Script>
    >
    > <!-- EMBEDED SOUNDS//-->
    ><embed src="frog.wav" autostart="fals e" loop="false"
    >hidden="true " name="frog">
    >
    ><embed src="bird.wav" autostart="fals e" loop="false"
    >hidden="true " name="bird">[/color]

    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.
    [color=blue]
    ><a href="javascrip t:playSound('fr og', 3, 4500);">FROG</a>[/color]
    <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.
    [color=blue]
    >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.[/color]

    No, that - playSound - function would either fail, or if it worked
    (given a very faulty ECMA script implementation) it would loop
    indefinitely.
    [color=blue]
    >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.[/color]

    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>[color=blue]
    > 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, ...[/color]
    <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.


    Comment

    Working...