简体   繁体   中英

Generate sound using JavaScript

I am trying to generate sound using JavaScript. I have used the following code

<html>
  <script type="text/javascript" src="jquery-1.4.2.js"></script>
  <script>
    function PlaySound(soundObj) {
      var sound = document.getElementById(soundObj);
      sound.Play();
    }

    function init() {
      //alert("");
      PlaySound('sound1')
    }

    window.onload = init;
  </script>

  <body>
    <form name="myform">
      <input type=button id="b1" name="b1name" value="Play Sound" onClick="PlaySound('sound1')">
    </form>
    <a href="#" onMouseOver="PlaySound('sound1')">Move mouse here</A>
    <embed src="beep-5.wav"  autostart="false" width="0" height="0" id="sound1" enablejavascript="true">
  </body>
</html>

Sound is being generated on button click and on mouseover. It is not being generated in the init function. If I call the below function in another JavaScript function, it does not work. Another point is that if I keep alerting before calling, then sound comes.

PlaySound('sound1')

I have tried using $("#b1").click(); (button click in JavaScript) but it's not working.

I know this is duplicate of this question , but the answer there did not work for me. I am really confused. Please help out.

Can I play this sound twice at a time?

The sound file may not have finished loading when init is called, but if you include an alert or when you manually click a button, there is enough time in between for the browser to have loaded the file.

That being said, embed is a non-standard and deprecated tag, and you really shouldn't be using it for playing sounds. Have a look at the HTML5 audio tag instead.

If you want a web page to play a sound via JavaScript, and you want the page to:

  • validate
  • work in all modern browsers
  • work across multiple platforms
  • work without plugins

The answer is simple: you can't do it. End of story.

Sure, you can come up with an example that works in one version of one browser on one platform, but I'll guarantee you: it won't work everywhere.

  1. a fast and dirty way (it also compatible with old browsers, even IE5) is to use can embedded a small wave file inside your javascript which then could be played as a resources (without saving to actual file), use binary encoding (same as embedding PNG into JS). a better way is building a JS Audio object , playing a bit (with buffer) that can be generated any frame-sound you'll like...

  2. use JS Audio Object

     var output = new Audio(); output.mozSetup(1, 44100); var samples = new Float32Array(22050); for (var i = 0, l = samples.length; i < l; i++) { samples[i] = Math.sin(i / 20); } 
    (also here)

If we generate sound using jquery sound plug in, http://plugins.jquery.com/project/sound_plugin playing sound on start up/java script without much delay. Working fine in IE and firefox.

By Introducing delay according to comment by casablanca, sound is playing in java script.Here is the code i have added: This referring link Introduce delay

function callback(){
    return function(){
 PlaySound('sound1');

    }
}
function init() {
  //  alert("");
setTimeout(callback(), 500);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM