Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Play sound in javascript

    I have a self project I am exploring. I would like to play a simple sound file each time a function is run. The function runs every second, so, basically, I am just trying to get an audible click sound file to run every time the function runs. I do not know how to play a sound file thru javascript. Any ideas?

    Here is the code file thus far...

    Code:
    <html>
    
    <head>
    
       <title>Timer</title>
       <link href="timer.css" rel="stylesheet" type="text/css" />
       <embed src="click.wav" autostart=false hidden=true name="sound1" enablejavascript="true">
       <script type="text/javascript">
           var seconds = 0;
           var clockId;
           var running = false;
    
           function runClock() { 
               seconds++;
               document.timer.timerClock.value = seconds; 
           }
    
           function startClock() {
               if (!running) {
               clockId = setInterval('runClock()',1000);
               running = true;
               }
           }
    
           function stopClock() {
                if (running) {
               clearInterval(clockId); 
               running = false;   
               }       
           }
           
           function resetClock() {
               document.timer.timerClock.value = 0;
               seconds = 0;
           }
       </script>
    </head>
    <body>
    <form id="timer" name="timer" action="">
    <div id="header">  
       <p>
       <span>Timer<br />   
       </p>
    </div>
    <div id="intro">
       <p>Click to begin timer</p>
       <p id="buttons">
          <input onclick="startClock()" type="button" value="Begin Seconds Counter" />
          <br />
          <input name="timerClock" id="timerClock" value="0" />      
          <br />
          <input onclick="stopClock()" type="button" value="Stop Timer" />
          <br />
          <input onclick="resetClock()" type="button" value="Reset Timer" />
       </p>
    </div>
    </form>
    </body>
    </html>
    Last edited by iceregent; 04-27-2011 at 06:31 PM. Reason: add code

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    I don't mean to sound rude, but did you try google first? have you tried some different methods and they didn't work?

    - http://www.phon.ucl.ac.uk/home/mark/audio/play.htm
    http://www.w3schools.com/media/media_browsersounds.asp
    http://webdesign.about.com/od/sound/...sound_oncl.htm

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well yes, i have tried google first. But hey, what do you think that THIS forum is for?

    And no, none of the things i have come across worked.

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I always search thru google first, and i have spent literally hours just today trying to make something work with this. I have a file called 'click.wave' that i want to have play each time the runClock() function runs.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by iceregent View Post
    II would like to play a simple sound file each time a function is run. The function runs every second, so, basically, I am just trying to get an audible click sound file to run every time the function runs. I do not know how to play a sound file thru javascript. Any ideas?
    That is a very good way to annoy your visitors and ensure that they leave your site quickly, never to return. Have you thought that people may be listening to music already when they come to your site?

    Cross-browser sounds are full of pitfalls, and best avoided for technical reasons also.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #6
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    uhm, this is not an online website for visitors. I simply am asking for help with my coding problem, not asking for advice on whether or not people will be annoyed.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    The code given in http://webdesign.about.com/od/sound/...sound_oncl.htm
    works in most browsers. If it does not work for you, then the fault, dear Brutus, lies not in the stars ......

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I guess I will look at another forum where I can get friendly help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •