Playing an audio file using Javascript

Discussion in 'Javascript' started by laredotornado@zipmail.com, Jun 29, 2005.

  1. Guest

    Hello, I had been given this code to play a single sound file embedded
    in a page. Sadly, the code doesn't actually "play" any sound file.
    Could you help supply some JS code that will play a sound file for both
    IE and Mozilla?

    Thanks, - Dave

    <html>
    <head>
    <title>Make some noize</title>

    <script type="text/javascript">
    <!--

    var Sound = new Object();
    Sound.play = function Sound_play(src) {
    if (!src) return false;
    this.stop();
    var elm;
    if (typeof document.all != "undefined") {
    elm = document.createElement("bgsound");
    elm.src = src;
    }
    else {
    elm = document.createElement("object");
    elm.setAttribute("data",src);
    elm.setAttribute("type","audio/x-wav");
    elm.setAttribute("controller","true");
    }
    document.body.appendChild(elm);
    this.elm = elm;
    return true;
    };

    Sound.stop = function Sound_stop() {
    if (this.elm) {
    this.elm.parentNode.removeChild(this.elm);
    this.elm = null;
    }
    };

    //-->
    </script>
    </head>

    <body>
    <div
    onmouseover="Sound.play('klatschn.wav')"
    onmouseout="Sound.stop()">Come over me</div>
    </body>
    </html>
     
    , Jun 29, 2005
    #1
    1. Advertising

  2. wrote:
    > Hello, I had been given this code to play a single sound file embedded
    > in a page.


    Yes, that was me given you that code.
    Have you included your sound file into the call here:

    > onmouseover="Sound.play('klatschn.wav')"


    Daniel
     
    Daniel Kirsch, Jun 29, 2005
    #2
    1. Advertising

  3. Guest

    Oops, I forgot that line.

    Everything works great on all platforms, browsers. Thanks! -
     
    , Jun 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Kremser
    Replies:
    1
    Views:
    667
    Thomas Matthews
    Oct 27, 2003
  2. aeonsun
    Replies:
    2
    Views:
    449
    Luc The Perverse
    Mar 11, 2007
  3. Sobin Thomas

    Doubt-Playing audio file in asp.net

    Sobin Thomas, May 21, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    394
    Sobin Thomas
    May 21, 2008
  4. Sobin Thomas

    Playing audio file in asp.net???

    Sobin Thomas, May 22, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    1,407
    George Ter-Saakov
    May 22, 2008
  5. Sobin Thomas

    Playing an audio file in our site???

    Sobin Thomas, May 23, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    342
Loading...

Share This Page