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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Playing sound with java script

    I wrote the following small HTML page, trying to play a sound in Firefox 8 and Internet Explorer 9. The button and the <a></a> link work fine, but calling from the <script></script> tag does nothing. Does anybody know, what can be wrong, and what I can do? I'm sorry, you have to supply the oef.wav file, since I can't upload it.

    Code:
    <html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
       <meta name="GENERATOR" content="Mozilla/4.05 [en] (Win95; I) [Netscape]">
       <meta name="Author" content="Torben Amtrup">
       <title>Soundtest</title>
    
    <script>
    function EvalSound(soundobj) {
      var thissound=document.getElementById(soundobj);
      thissound.Play();
    }
    </script>
    </head>
    
    <body>
    <embed src="oef.wav" width="1" height="1" id="sound1" enablejavascript="true" autostart="false">
    
    <form>
      <input type="button" value="Play Sound" onclick="EvalSound('sound1')">
    </form>
    
    <a onclick="EvalSound('sound1')">Play Sound</a>
    
    <script language="JavaScript">
      EvalSound('sound1');
    </script>
    
    </body>

  • #2
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    354
    Thanks
    9
    Thanked 52 Times in 51 Posts
    Try to give it some microseconds, like:
    <script>
    setInterval("EvalSound('sound1')",1000);
    </script>

    I'm not sure why though

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Cross-browser scripting for playing sounds is a nightmare.

    http://sodeve.net/2011/10/how-to-add...-all-browsers/

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    its probably that the sound file has not loaded when the inline <script> tries to play it, whereas by the time you get around to clicking the link/button the file has already loaded and plays just fine.

    you might be able to do something like:
    Code:
    <script>
      window.onload=function(){  
         EvalSound('sound1'); 
      };
    </script>
    if that doesn't work, i would recommend jplayer (a jquery plugin) for (relatively) simple audio playback functionality that works on all browsers and devices like tablets and phones.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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