views:

134

answers:

5

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?

A: 

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
How to use this in my scenario? THank you for help
vishnu
See this page for some examples: [HTML5 Audio and JavaScript Control](http://www.storiesinflight.com/html5/audio.html)
casablanca
Thank you. But this tag is not supported by IE8 correct
vishnu
You're right about that. At the moment, the only safe way to support all browsers is to use Flash.
casablanca
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
@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
Can you provide us example to use flash? this flash plays without deplay?
vishnu
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
@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
A: 

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);
 }
vishnu
This introduced delay is not working properly in firefox
vishnu
A: 

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
Well, if you define *modern* as not IE, `<audio>` works perfectly well
Yi Jiang
Sadly, most of us cannot—the best we can do is say no to IE6.
Dori
A: 
  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)

Elad Karako
This Audio(); is recognizing only fire fox. but not ie?
vishnu
A: 

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