我想我的网页哔哔每当用户超过我的<textarea>的最大字符限制。


当前回答

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

一个例子:

<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代码中调用它,如下所示:

PlaySound("sound1");

这应该完全是你想要的-你只需要自己找到/创建哔哔声,这应该是微不足道的。

其他回答

该代码支持多个哔哔声的排序,以及频率的逐渐变化('ramp'标志)。下面给出了两个例子。

<script>
var audioContext = null;
var oscillatorNode = null;
var stopTime = 0;

function beep (frequency, durationSec, ramp=false)
{
    if (oscillatorNode == null) {
        audioContext = new (window.AudioContext || window.webkitAudioContext) ();
        stopTime = audioContext.currentTime;

        oscillatorNode = audioContext.createOscillator();
        oscillatorNode.type = "sine";
        oscillatorNode.connect (audioContext.destination);
        if (ramp) {
            oscillatorNode.frequency.setValueAtTime (frequency, stopTime);
        }
        oscillatorNode.start ();
        oscillatorNode.onended = function() {
            oscillatorNode = null;
            audioContext = null;
        }
    }

    if (ramp) {
        oscillatorNode.frequency.linearRampToValueAtTime (frequency, stopTime); // value in hertz
    } else {
        oscillatorNode.frequency.setValueAtTime (frequency, stopTime);  // value in hertz
    }

    stopTime += durationSec;
    oscillatorNode.stop (stopTime);
}
function test1()
{
        beep (250, 0.5);
        beep (1000, 0.2);
        beep (550, 0.5);
}
    
function test2()
{
        beep (50, 2, true);
        beep (5000, 2, true);
        beep (50, 0, true);
}
</script>
    
<button onclick='test1()'>Beep!</button>
<button onclick='test2()'>Beep(ramped)!</button>

没有跨浏览器的方法来实现这纯javascript。相反,您可以使用一个小的.wav文件,使用嵌入或对象标记播放。

/*if you want to beep without using a wave file*/
var context = new AudioContext();
var oscillator = context.createOscillator();
oscillator.type = "sine";
oscillator.frequency.value = 800;
oscillator.connect(context.destination);
oscillator.start(); 
// Beep for 500 milliseconds
setTimeout(function () {
    oscillator.stop();
}, 100);                
            

上面的答案在当时是正确的,但现在是错误的;你可以用纯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。它在IE上不受支持,但微软已经停产了。如果您在特定的浏览器上有任何问题,请报告。

当我们读到这个答案时,HTML5将为你解决这个问题,如果你愿意这样做的话。所有现代浏览器都支持HTML5音频。

下面是示例的副本:

var snd = new Audio("file.wav"); // buffers automatically when created
snd.play();

function beep(freq = 660, duration = 90, vol = 50) { var context = new(window.AudioContext || window.webkitAudioContext); const oscillator = context.createOscillator(); const gain = context.createGain(); gain.gain.setValueAtTime(0, context.currentTime); gain.gain.linearRampToValueAtTime(1, context.currentTime + 0.002); oscillator.connect(gain); oscillator.frequency.value = freq; oscillator.type = "square"; gain.connect(context.destination); oscillator.start(context.currentTime); oscillator.stop(context.currentTime + duration * .001); oscillator.onended = () => context.close(); } <br> <center><button onclick="beep()">Beep!</button></center>