简体   繁体   English

使用JavaScript产生声音

[英]Generate sound using JavaScript

I am trying to generate sound using JavaScript. 我正在尝试使用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. 它不是在init函数中生成的。 If I call the below function in another JavaScript function, it does not work. 如果我在另一个JavaScript函数中调用下面的函数,它将无法正常工作。 Another point is that if I keep alerting before calling, then sound comes. 另一点是,如果我在打电话之前一直保持警觉,那么声音就来了。

PlaySound('sound1')

I have tried using $("#b1").click(); 我尝试使用$("#b1").click(); (button click in JavaScript) but it's not working. (在JavaScript中单击按钮),但是它不起作用。

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. 调用init时,声音文件可能尚未完成加载,但是如果包含alert或手动单击按钮,则在这之间有足够的时间供浏览器加载文件。

That being said, embed is a non-standard and deprecated tag, and you really shouldn't be using it for playing sounds. 话虽这么说, embed是一个非标准且已弃用的标签,您实际上不应该将其用于播放声音。 Have a look at the HTML5 audio tag instead. 改用HTML5 audio标签

If you want a web page to play a sound via JavaScript, and you want the page to: 如果您希望网页通过JavaScript播放声音,并且希望该网页执行以下操作:

  • 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). 一种快速而又肮脏的方法(它也兼容旧的浏览器,甚至IE5)是使用可以在JavaScript中嵌入一个小wave文件,然后将其作为资源播放(不保存到实际文件中),使用二进制编码(与将PNG嵌入到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... 更好的方法是构建一个JS Audio对象 ,播放一点(带有缓冲区),该点可以生成您想要的任何帧声音...

  2. use JS Audio Object 使用JS音频对象

     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. 如果我们使用jquery声音插件生成声音,则http://plugins.jquery.com/project/sound_plugin会在start / java脚本上播放声音而不会产生太多延迟。 Working fine in IE and firefox. 在IE和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 通过根据casablanca的注释引入延迟,声音正在Java脚本中播放。这是我添加的代码:此引用链接引入延迟

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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