首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使JavaScript发出蜂鸣音?

如何使JavaScript发出蜂鸣音?
EN

Stack Overflow用户
提问于 2009-05-18 18:36:20
回答 15查看 284.5K关注 0票数 280

当用户超过我的<textarea>的最大字符限制时,我希望我的网页发出蜂鸣声。

EN

回答 15

Stack Overflow用户

回答已采纳

发布于 2009-05-18 18:42:24

这不可能直接在JavaScript中完成。您需要在HTML中嵌入一个简短的WAV文件,然后通过代码播放该文件。

举个例子:

代码语言:javascript
运行
复制
<script>
function PlaySound(soundObj) {
  var sound = document.getElementById(soundObj);
  sound.Play();
}
</script>

<embed src="success.wav" autostart="false" width="0" height="0" id="sound1"
enablejavascript="true">

然后您可以从JavaScript代码中调用它,如下所示:

代码语言:javascript
运行
复制
PlaySound("sound1");

这应该会做你想要的-你只需要自己找到/创建蜂鸣声,这应该是微不足道的。

票数 148
EN

Stack Overflow用户

发布于 2015-04-15 11:38:30

上面的答案在当时是正确的,但现在是错误的;你可以用纯javascript来做。但是,使用javascript的答案不再有效,其他答案非常有限,或者不使用纯javascript。

我做了我自己的解决方案,效果很好,可以让你控制音量,频率和波型。

代码语言:javascript
运行
复制
//if you have another AudioContext class use that one, as some browsers have a limit
var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext);

//All arguments are optional:

//duration of the tone in milliseconds. Default is 500
//frequency of the tone in hertz. default is 440
//volume of the tone. Default is 1, off is 0.
//type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine.
//callback to use on end of tone
function beep(duration, frequency, volume, type, callback) {
    var oscillator = audioCtx.createOscillator();
    var gainNode = audioCtx.createGain();

    oscillator.connect(gainNode);
    gainNode.connect(audioCtx.destination);

    if (volume){gainNode.gain.value = volume;}
    if (frequency){oscillator.frequency.value = frequency;}
    if (type){oscillator.type = type;}
    if (callback){oscillator.onended = callback;}

    oscillator.start(audioCtx.currentTime);
    oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
};

有人建议我编辑这段代码,指出它只适用于某些浏览器。然而,据我所知,所有现代浏览器上都有Audiocontext seems to be supported。它在IE上不受支持,但微软已经停止使用。如果您在特定浏览器上遇到任何问题,请报告。

票数 98
EN

Stack Overflow用户

发布于 2012-11-02 19:12:17

我用新的Audio API编写了一个函数来发出蜂鸣声。

代码语言:javascript
运行
复制
var beep = (function () {
    var ctxClass = window.audioContext ||window.AudioContext || window.AudioContext || window.webkitAudioContext
    var ctx = new ctxClass();
    return function (duration, type, finishedCallback) {

        duration = +duration;

        // Only 0-4 are valid types.
        type = (type % 5) || 0;

        if (typeof finishedCallback != "function") {
            finishedCallback = function () {};
        }

        var osc = ctx.createOscillator();

        osc.type = type;
        //osc.type = "sine";

        osc.connect(ctx.destination);
        if (osc.noteOn) osc.noteOn(0); // old browsers
        if (osc.start) osc.start(); // new browsers

        setTimeout(function () {
            if (osc.noteOff) osc.noteOff(0); // old browsers
            if (osc.stop) osc.stop(); // new browsers
            finishedCallback();
        }, duration);

    };
})();

jsFiddle

票数 87
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/879152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档