0

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?

Community
  • 1
  • 1
vishnu
  • 567
  • 3
  • 14
  • 34

5 Answers5

0

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.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • How to use this in my scenario? THank you for help – vishnu Oct 20 '10 at 19:19
  • See this page for some examples: [HTML5 Audio and JavaScript Control](http://www.storiesinflight.com/html5/audio.html) – casablanca Oct 20 '10 at 19:26
  • You're right about that. At the moment, the only safe way to support all browsers is to use Flash. – casablanca Oct 20 '10 at 19:45
  • I need only two beeps on error. It i introduce some delay, it is working. But can i repeat the same sound two times. Can i do that – vishnu Oct 20 '10 at 19:50
  • @casablanca - *"the only safe way to support all browsers is to use Flash"* - except that not all browsers have Flash (e.g., iPhones, iPads, and most Androids). So no, Flash won't do it either. – Dori Oct 21 '10 at 02:41
  • Can you provide us example to use flash? this flash plays without deplay? – vishnu Oct 21 '10 at 02:56
  • This soution i have used is supporting cross browser(ie,firefox,opera) as discussed in http://www.phon.ucl.ac.uk/home/mark/audio/play.htm – vishnu Oct 21 '10 at 02:59
  • @vishnu - that page says (at the bottom) that it was "Last revised 23 March 2005". It doesn't mention Chrome, Safari, or mobile browsers. The latest version of IE tested was 6. The latest version of FF tested was 1.0. The author only tested on Windows. You're wasting your time. – Dori Oct 21 '10 at 22:17
0

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.

Dori
  • 915
  • 1
  • 12
  • 20
0
  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)

Community
  • 1
  • 1
0

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.

vishnu
  • 567
  • 3
  • 14
  • 34
-1

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);
 }
Community
  • 1
  • 1
vishnu
  • 567
  • 3
  • 14
  • 34